javascript - Basic Express Routes -
how following routes work in express:
/ (get , post) /users (get , post)
right now, when visit /users, page renders correctly, on submission, runs code in /index (post route) instead of code in /users (post route).
files this: users.js:
router.get('/', function(req, res, next) { var title = 'users'; res.render('users'); }); router.post('/',function(req,res){ ....});
index.js:
router.get('/', function(req, res, next) { var title = 'index'; res.render('index'); }); router.post('/post',function(req,res){
app.js:
var routes = require('./routes/index'); var users = require('./routes/users'); var show = require('./routes/show');
and in app.use section:
app.use('/users', users); app.use('/show', show); app.use('/', routes);
edit:
form action is:
form(method="post", action="/post")
like nicholas has said in comments, should update form follows:
form
form(method="post", action="/users")
this hit post route in users controller.
if desire post /
well, should update index.js
to:
index.js
// should / not /post router.post('/',function(req,res){ ... }
also, aware you'll need use body-parser parse out form data.
hope helps!
Comments
Post a Comment