app.js里面加载了login.js的路由 var login = require(’./routes/login’); app.use(’/login’, login);
为什么login.js里面的路由不起作用,必须写在index.js里面吗?
下面是login.js的代码
var express = require(‘express’); var DBUtil = require(’./DBUtil.js’); var router = express.Router();
/* GET home page. / router.get(’/’, function(req, res, next) { res.render(‘index’, { title: ‘首页’ }); }); / 登陆. */ router.get(’/customer/login’, function(req, res, next) { res.render(‘login’, { title: ‘登陆’ }); }); router.post(’/customer/login’,function(req,res){ console.log(‘登陆 begin…’); var code; var message; var username = req.body[‘cid’]; var password = req.body[‘pwd’]; var loginSQL = ‘SELECT 1 FROM customer WHERE customer_name = ’ + ‘’’ + username + ‘’’ + ‘AND customer_pwd =’ + ‘’’ + password + ‘’’ ; console.log(loginSQL); DBUtil.query(loginSQL,function(dbResult){ if( dbResult != null && dbResult.length > 0 ){ code = ‘10000’; message = ‘OK!!’; }else{ code = ‘10040’; message = ‘Failure!!’; } var result ={ code : code, message : message }; res.send({‘result’: result}); }); }) module.exports = router;
看一下router的文档应该就能明白了http://expressjs.com/zh/4x/api.html#router
app.use('/login’, login);
只会将 /login'
下的请求传入login.js
所以按照你的写法
'/login'
对应index
post/login/customer/login
才会执行登陆逻辑
@sjfkai 原来如此,太感谢了