创建node服务端

master
liuyx 2 years ago
parent d4872b4697
commit 399d2a985d

24
server/.gitignore vendored

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

@ -0,0 +1,28 @@
// const express = require('express')
// const bodyparse = require('body-parser')
// const routes = require('./routes')
import express from 'express'
import bodyparse from 'body-parser'
import routes from './routes/index.js'
import mongoose from 'mongoose'
mongoose.connect('mongodb://localhost/test', (err) => {
if (err) {
console.log('error')
} else {
console.log('连接成功')
}
})
// 创建服务
const app = express()
// 解析数据
app.use(bodyparse.json())
// 路由
routes(app)
// 监听3000端口
app.listen(3000, () => {
console.log('this server are running on http://localhost:3000')
})

@ -0,0 +1,20 @@
/*
七牛云配置
*/
// const qiniu = require('qiniu')
import qiniu from 'qiniu'
// 创建上传凭证
const accessKey = 'et7Cr4yqTf5kHJ3cNuAkzAghjGI378X9tflGO6dT' // 这里填写七牛云的accessKey
const secretKey = '1eKk1NJIeXmuZ6klkvaLmvRqVQeuO817G_6AOblD'// 这里填写七牛云的secretKey
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const options = {
scope: 'liuyxcc-blog', // 这里填写七牛云空间名称
expires: 60 * 60 * 24 * 7
}
const putPolicy = new qiniu.rs.PutPolicy(options)
const uploadToken = putPolicy.uploadToken(mac)
export {
uploadToken
}

@ -0,0 +1,12 @@
// const mongoose = ('mongoose')
import mongoose from 'mongoose'
const ArticleSchema = new mongoose.Schema({
category: String,
content: String,
cover: String,
tagList: [],
title: String,
})
export default mongoose.model('Article', ArticleSchema)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,21 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"mongoose": "^6.7.5",
"qiniu": "^7.8.0"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}

@ -0,0 +1,27 @@
// const articleRouter = require('express').Router()
import express from 'express'
import Article from '../models/article.js'
const articleRouter = express.Router()
articleRouter.get('/articles', function(req, res, next) {
Article.find((err, data) => {
const {...copy} = data
res.status(200).json(copy)
})
})
articleRouter.get('/:id', (req, res) => {
const article = {
id: req.params.id,
cover: 'https://typora-lyx.oss-cn-guangzhou.aliyuncs.com/typora/wallhaven-6dqemx.jpg',
title: '测试',
content: '## 这是一篇测试文章\noh yes~',
tagList: ['Web', 'Vue3', 'Pinia'],
category: '学习笔记'
}
res.status(200).json(article)
})
// module.exports = articleRouter
export default articleRouter

@ -0,0 +1,16 @@
// const token = require('./token')
// const article = require('./article')
import token from './token.js'
import article from './article.js'
// module.exports = routers = (app) => {
// app.use('/token', token)
// app.use('/article', article)
// }
const routes = (app) => {
app.use('/token', token)
app.use('/article', article)
}
export default routes

@ -0,0 +1,13 @@
// const tokenRouter = require('express').Router()
// const qnconfig = require('../config') // 引入七牛云配置
import express from 'express'
import { uploadToken } from '../config.js'
const tokenRouter = express.Router()
tokenRouter.get('/qiniu', (req, res, next) => {
res.status(200).send(uploadToken)
})
// module.exports = tokenRouter
export default tokenRouter
Loading…
Cancel
Save