Currently, I am utilizing vue.js to develop an application and have created a mock login api on localhost in dev-server.js. Now, I am looking to refactor the code related to the login api into a separate file. Do you have any suggestions on how I can achieve this? Additionally, there are some code snippets related to CORS. Here is the existing 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 the password is not correct, please try again'
});
}
}
app.use('/api', apiRoutes);