index.js
const express = require('express');
const app = express()
// 导入路由模块
const router = require('./router/index')
// 注册路由模块
app.use(router)
app.listen(3000, ()=>{
console.log('http://127.0.0.1:3000')
})
router/index.js
const express = require('express');
const router = express.Router();
// 挂载具体路由
router.get('/getUserInfo', (req, res)=>{
req.send('获取用户信息')
})
router.post('/addUserInfo', (req, res)=>{
req.send('添加用户信息')
})
module.exports = router
给接口地址添加统一的访问前缀
const express = require('express');
const app = express()
// 导入路由模块
const router = require('./router/index')
// 注册路由模块
// 添加统一的访问前缀
app.use('/api', router)
app.listen(3000, ()=>{
console.log('http://127.0.0.1:3000')
})