DolDurma
DolDurma

Reputation: 17331

Nodejs Cannot GET /helloworld in simple application

This below code is my first Express restfull application. After running server.js I expected after entering http://localhost:3020/helloworld url on FireFox I get Hello, World! message, but I get this message:

Cannot GET /helloworld 

code:

var express = require('express');
var router = express.Router();
var app = express();
var server = require('http').createServer(app);
var port = process.env.PORT || 3020;

/* GET home page. */
router.get('/helloworld', function(req, res) {
    res.render('helloworld', { title: 'Hello, World!' });
});

server.listen(port, function () {
    console.log('Server listening at port %d', port);
});

Upvotes: 0

Views: 1065

Answers (1)

Naeem Shaikh
Naeem Shaikh

Reputation: 15715

You are defining the route on the router, not on the app.

var router = express.Router();

is a router, different than app.

var app = express();

What you are doing wrong, is you are not mounting the router on the app where the /helloworld route is defined.

do either of the following:

app.get('/helloworld', function(req, res) {
    res.render('helloworld', { title: 'Hello, World!' });
});

Or else add the following line in your app:

app.use('/',router);

Upvotes: 5

Related Questions