0π
β
(I think itβs a question about node and express, instead of vue.js)
While express is basically built web app with middleware, I think itβs time to split your logic into different middleware.
So you can put your login logic into a independent .js file as a 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 the password is not correct, please try again'
});
}
};
module.exports = loginMiddleware;
Then requiring it from your app like:
// app.js
const loginMiddleware = require('./login');
// ...
apiRoutes.post('/user/login', loginMiddleware);
Here is the official express document about how to write your middleware properly: https://expressjs.com/en/guide/using-middleware.html
0π
Maybe check out a module bundler like webpack. It allows you to split code into different bundles which can be loaded together.
Source:stackexchange.com