javascript - How to split the dev-server.js file in vue to several separated files? -


i using vue.js compose app, , mocked login api of localhost in dev-server.js, want separate code login api independent file, should do? besides there code cors, here code:

var app = express() var bodyparser = require('body-parser') var multer = require('multer') var upload = multer()  app.use(bodyparser.json()) app.use(bodyparser.urlencoded({extended: true})) // cors var allowcrossdomain = function (req, res, next) {   res.header('access-control-allow-origin', 'http://localhost:8080')   res.header('access-control-allow-methods', 'get, post, put, delete')   res.header('access-control-allow-headers', 'content-type, x-token')   res.header('access-control-allow-credentials', 'true')   next() } app.use(allowcrossdomain)    // mock localhost api var apiroutes = express.router() // login api; const useraccountlist = ['100000', '100001', '100002', '100003'] apiroutes.post('/user/login', upload.array(), function (req, res) {   if (useraccountlist.indexof(req.body.account) < 0){     return res.json({       code: 50000,       msg: 'the account or password not correct, please try again'     });   } } app.use('/api', apiroutes); 

(i think it's question node , express, instead of vue.js)

while express built web app middleware, think it's time split logic different middleware.

so can put login logic independent .js file middleware like:

// login.js  const useraccountlist = ['100000', '100001', '100002', '100003']  const loginmiddleware = function (req, res, next) {   if (useraccountlist.indexof(req.body.account) < 0){     res.json({       code: 50000,       msg: 'the account or password not correct, please try again'     });   } };  module.exports = loginmiddleware; 

then requiring app like:

// app.js  const loginmiddleware = require('./login');  // ...  apiroutes.post('/user/login', loginmiddleware); 

here official express document how write middleware properly: https://expressjs.com/en/guide/using-middleware.html


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -