0

I am following a tutorial to build a node/express app using the jade templating engine and I am getting a 404 error when I try to submit a form. For my routing I have the line app.post('/sign_up', sign_up); which gives me an error. The sign_upvariable is declared above using this line var sign_up = require('./routes/sign_up');.

The strange thing is if I register a callback function in the place of the sign_up variable then I can retrieve the data fine, which I did using this code app.post('/sign_up', function(req, res){ console.log(req.body); });.

In my routes folder I have got the sign_up.js file and which outputs a rather simple line of text. I have tried changing the function

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('You signed up');
});
module.exports = router;

Below are the contents of my form.jade file

    extends layout
    block content
    h1= title
    form(name="sign-up", action="/sign_up", method="post")
        div
            label Username: 
            input(type="text", name="username")
        div
            label Password: 
            input(type="password", name="password")
        div
            input(type="submit", value="Sign Up")
Courtney
  • 309
  • 2
  • 10
  • Take a look at the question I think this is a duplicate of. You are confused about the way you declare routes in external files. – Robert Moskal Apr 20 '16 at 14:21
  • Thanks for the input. Like I said I am following a tutorial and that was what is in the tutorial. Thank you however for pointing in the right direction. – Courtney Apr 20 '16 at 15:13

1 Answers1

0

If you are using sign_up.js to handle the routing, then you should define the post function there. Try to add this in sign_up.js

/* POST*/
router.post('/sign_up', function(req, res, next) {
  res.send('You signed up');
});
Anthony C
  • 2,117
  • 2
  • 15
  • 26
  • This solved me problem. Thank you very much. Its odd because the other paths all used '/' and they worked fine so in the users.js file the '/users' path is defined as `router.get('/', function(req, res, next) { res.send('respond with a resource'); });` Can you explain why that works and the other doesn't? – Courtney Apr 20 '16 at 15:10
  • The others are using `router.get`, for post method, you need to use `router.post`. If you need Put, in the future, you will need to use `router.put` as well. If you need to have a global handler for all http methods, you can use `router.all` too. – Anthony C Apr 20 '16 at 15:13
  • What I was asking was why is it you can navigate to _http://localhost:3000/users_ even the the route for that path is `router.get('/', function(req, res, next) { res.send('respond with a resource'); });` which is inside the users.js file? When I put `router.get('/users', function(req, res, next) { res.send('respond with a resource'); });` in the users.js file, I get a 404 error. – Courtney Apr 20 '16 at 19:09
  • do you have `app.use('/users', users);` in app.js? – Anthony C Apr 20 '16 at 19:15
  • Yes I do, I also had `app.post('/sign_up', sign_up);`. Is it that syntax cannot work? It appears to be a really poor tutorial, I guess I will just continue and see what I can learn for now. Have you got any suggestions for any better tutorials that I can use. – Courtney Apr 20 '16 at 19:31
  • I think i was using the same tutorial too. When you pass the url in `app.use()`, that becomes the basepath of the route. So if you have `/users` in app.js and also in the router, the route it binds to actually is `/users/users`, you only need '/users' in one or the others, but not in both places. – Anthony C Apr 20 '16 at 19:40