express mockjs 模拟前端数据

npm init -y
npm i express
npm i mockjs
npm i nodemon

/index.js

const mockjs = require('mockjs')

const express = require('express')
const app = express()
const router = express.Router()
const user = require('./routes/user')

app.use('/user', user)

app.listen(5140, () => {
    console.log('start in 5140');
})

/routes/user.js

const express = require('express');
const mockjs = require('mockjs')

let router = express.Router();

router.get('/', function (req, res) {
    res.send('用户首页');
});

router.get('/:id', function (req, res) {
    res.json(mockjs.mock({
        id: 1,
        name: '@cname',
        address: '@county(true)'
    }))
});

module.exports = router;
"scripts": {
    "start": "nodemon ."
},

你可能感兴趣的:(javascript,npm,前端)