logo

Backend 2

 
notion image

router模块化思想

上节课backend_class_1根据features创建了不同的role. 其中回调函数async function要提取出放到新文件夹Controllers中.
notion image

课上代码

backend根目录下index.js (可以用server.js避免歧义)

//npm init //npm i express cors axios json-server //npm i nodemon -D //npm run json:server //npm run server (npm nodemon index.js)在package.json script里面修改 const express = require("express"); const cors = require("cors"); const router = require("./routes/index"); const { handleErrors } = require("./middleware/errorMiddleware"); const app = express(); app.use(cors()); // 解决跨域问题 app.use(express.json()); //允许解析使用json数据 //router需要挂在在数据解析(json)和跨域全局中间件(cors)后面 app.use("/api", router); // 提取公共部分, 这样每个路由不需要重复写"/api" // use error middleware at the end (全局中间件) app.use(handleErrors); const PORT = 80; app.listen(PORT, function () { console.log("Server is running on http://localhost:80"); }); //app.use(express.json())// parse JSON requests //app.use(express.urlencoded({extended:true})) //parse URL-encoded requests //app.use(cors()) //必须在路由之前挂在,解决跨域问题 //app.use(authMiddleware);// custom authentication middleare 比如用户验证,api授权之类的 //app.use(routes) // route handlers //app.use(handleErrors) // error handling middleware (at the end) error中间件在路由后挂载
本章目录
    1v1免费职业咨询