1

I started to learn nodejs and installed express with their documentation. Now on the process, i want to create a login form and on clicking log-in i want it to pass to check route to check the credentials.

I am not posting the full code here. Just the part of it.

var routes = require('./routes/index'); //default with express
var users = require('./routes/users'); //default with express
var login = require('./routes/login'); //created by me
var check = require('./routes/check'); //created by me.

Update 2

app.use('/', routes);
app.use('/users', users);
app.use('/login', login);
app.use('/check', check);

The login route is working fine and i am getting the output. When i click login, it enters the route

http://localhost:3000/check

and throws 404 not found.

Whereas if i directly enter the url in my browser i am getting the output in the browser.

check.js

router.get('/', function(req, res, next) {
res.send('respond with a resource');
});

updating check.js

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

/* GET users listing. */
router.post('/check', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

login.jade

extends layout
  block content
    div(class="col-md-4 col-lg-4 col-md-offset-4 col-lg-offset-4")
    h1 User Login
    form(name="login", action="/check", method="post")
      div(class="form-group")
        input(type="text", name="user" class="form-control")
      div(class="form-group")
        input(type="password", name="pass" class="form-control")
      div(class="form-group")
        input(type="submit", value="login" class="btn btn-warning form-control")

I am also looking for basic documentation to create a node.js application, that is not complicated as well and easy to understand for a noob.

Update 1: Error while clicking with login button

Error: Not Found
    at app.use.res.render.message (/home/alaksandar/Desktop/Untitled Folder/myapp/app.js:38:13)
    at Layer.handle [as handle_request] (/home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/layer.js:82:5)
    at trim_prefix (/home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:302:13)
    at /home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:270:7
    at Function.proto.process_params (/home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:321:12)
    at next (/home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:261:10)
    at /home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:603:15
    at next (/home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:246:14)
    at Function.proto.handle (/home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:166:3)
    at router (/home/alaksandar/Desktop/Untitled Folder/myapp/node_modules/express/lib/router/index.js:35:12)

2 Answers 2

1

Update

In app.js use

app.use('/check',check)

And in check.js use

 router.post('/', function(req, res, next) {
res.send('respond with a resource');
});

And be sure you are exporting this function using module.exports

old

In your check.js file you need to use

router.post('/check', function(req, res, next) {
res.send('respond with a resource');
});

Instead of

router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
Sign up to request clarification or add additional context in comments.

7 Comments

Please check my update. I already did this and posted with not luck.
Added update 2 which is the same as per your advise.
dont use '/check' inside the router.post in check.js , use '/' only
Thanks. It worked. But earlier it didnt work. No idea why it was not working earlier.
mayb because of app.get instead of app.post?
|
0

EDIT 2

the router has to be applied as middleware via app.use() :

router.post('/', function(req, res, next) {
res.send('respond with a resource');
});

app.use('/check', router);

then a POST Request on Path "/check" would be handeled by your router.

if you are not doing this provide the routing directly via app.post() like this

app.post('/check', function(req, res, next) {
res.send('respond with a resource'); 
});

EDIT:

action="/check" is enough ;)

But you are still missing the Fact that the form sends an POST to the Route "/check" and your express Router is listeing to a GET request on Route "/"

ORIGINAL:

I think u are using the action attribute of the form Tag wrong: it should be

action="http://www.yourDomain.com/check"

maybe i am wrong and

action="/check"

is enough,

but also you are listening for an GET Request and the form is sending a POST on submit.

router.get('/', function(req, res, next) {
res.send('respond with a resource');
});

Also the route you are listeing to is "/" and schould be "/check" my approach would be:

check.js

router.post('/check', function(req, res, next) {
res.send('respond with a resource');
});

login.jade

extends layout
block content
 div(class="col-md-4 col-lg-4 col-md-offset-4 col-lg-offset-4")
 h1 User Login
 form(name="login", action="http://www.yourDomain.com/check", method="post")
  div(class="form-group")
    input(type="text", name="user" class="form-control")
  div(class="form-group")
    input(type="password", name="pass" class="form-control")
  div(class="form-group")
    input(type="submit", value="login" class="btn btn-warning form-control")

Hope this helps

8 Comments

You definitely don't need to include the full domain name in a form action; if it's on the same domain then /check would be relative to that domain. The issue is the GET v. POST.
Nope. Its not working change to full domain or using POST too. I tested it even before i posted it.
i edited it again, the problem in my opinion is that u are listeing for a GET Request to "/" in express and the form is sending a POST to "/check".
Added an update of error output shown in the screen while clicking login button
@AlaksandarJesusGene Don't add an image, add the text.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.