app.js:
var routes = require('./routes');
app.use(express.urlencoded());
app.use(app.router);
app.get('/', routes.index);
And some more use statements.
./routes/index.js:
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
exports.impressum = function(req, res){
res.render('impressum', { title: 'Impressum' });
};
And in ./views/ I have index.jade and impressum.jade
Calling http://localhost:3000/ shows the index.
But I can't reach the impressum page at all. Any clues?
As damphat mentions you need to wire up a route to your impressum page. You'll have to put in a route for each page you want your app to respond to.
I did a screencast that goes into Express routing. You can check it out-- it might be of use. Also the Express documentation is useful.
Related
I would like to understand the order precedence in express.js. For example, as bellow code
var routes = require('./routes/index');
var users = require('./routes/users');
var api = require('./routes/api');
app.use('/api', api);
app.use('/users', users);
app.use('/:name', function(req, res, next) {
console.log('from app.js name:', req.params.name);
res.render('index', {
title: req.params.name
});
}, routes);
app.use('/', function(req, res, next) {
res.render('index', {
title: 'MainPage'
});
});
If a request come from client localhost:3000/api/abc and localhost:3000/user/abc, the response from api and user module. But if I make a request like localhost:3000/myName/xyz, the app module return a response. This behavior let me concern about what is precedence of expressjs and what is correct order for router modules. Why routers do not confuse between actions "api", "users" and parameter ":name". Please let me understand clearly how express does and what is precedence.
The order is first come first serve.
In your case, if user hits /api, he will get response from api, but if you write /:name route before /api, /:name will serve for /api requests also.
Case1: /api will serve requests for /api.
var routes = require('./routes/index');
var users = require('./routes/users');
var api = require('./routes/api');
app.use('/api', api);
app.use('/users', users);
app.use('/:name', function(req, res, next) {
console.log('from app.js name:', req.params.name);
res.render('index', {
title: req.params.name
});
}, routes);
app.use('/', function(req, res, next) {
res.render('index', {
title: 'MainPage'
});
});
Case2: /:name serves requests for /api and /users
var routes = require('./routes/index');
var users = require('./routes/users');
var api = require('./routes/api');
app.use('/:name', function(req, res, next) {
console.log('from app.js name:', req.params.name);
res.render('index', {
title: req.params.name
});
}, routes);
app.use('/api', api);
app.use('/users', users);
app.use('/', function(req, res, next) {
res.render('index', {
title: 'MainPage'
});
});
The example given in the ExpressJS documentation is pretty simple and was unfortunately difficult to find. I only found it through another SO question.
Middleware functions are executed sequentially, therefore the order of middleware inclusion is important
app.use(function(req, res, next) {
res.send('This is the only thing that ever happens')
}
app.use(...) // never used
app.get(...) // never used
app.put(...) // never used
app.post(...) // never used
when I enter in http://localhost/client, display 404.
app.get('/client', function(req, res) {
...
ejs.render('any template', {});
...
res.end();
});
app.get('*', function(req, res) {
console.log('404');
...
});
but if I remove "ejs.render" and put res.end('any html') works.
How can i use "ejs.render" and not call 404? Thanks. It's a bug.
You need to set ejs for use EJS with express.
The Express application generator uses Jade as its default, but it also supports several others (Like ejs, pug, etc).
For example:
var express = require('express');
var app = express();
// set the view engine to ejs
app.set('view engine', 'ejs');
// use res.render to load up an ejs view file
// index page
app.get('/client', function(req, res) {
res.render('pages/index'); //does not needs ejs extension
});
app.get('*', function(req, res){
res.send('what???', 404);
});
app.listen(8080);
console.log('8080 is the magic port');
When you make a request to the home page, the index.ejs file will be rendered as HTML.
See the official documentation here.
Important from my app.js file:
var express = require('express');
var profile = require('./controllers/profile-controller'); //require my controller (profile-controller.js)
app.use(express.static(path.join(__dirname, 'public')));
app.use('/user', profile);
When I go to 'mysite.com/user' - it works fine, but when I go to 'mysite.com/user/user1', my CSS and images ain't loading. Here is my controller file:
var express = require('express');
var router = express.Router();
// route for www.mysite.com/user
router.get('/', function(req, res, next) {
res.render('profile',{ title: 'Profile' });
});
// route for www.mysite.com/user/user1
router.get('/:id([A-Za-z0-9_]{5})', function(req, res, next) {
var id = req.params.id;
res.render('profile',{ title: 'Profile' });
});
module.exports = router;
Console gives me that:
So why I have the same route for both pages, but one of them working well, but second gives 404 for static files?
In your index.hbs file, you have src="images/wow.png", which is a relative path to your image resource. So when you go to /user/user1, it just adds the /user to your image resource path.
Instead of src="images/wow.png" try putting src="/public/images/wow.png" or src="/images/wow.png".
I would like to understand the order precedence in express.js. For example, as bellow code
var routes = require('./routes/index');
var users = require('./routes/users');
var api = require('./routes/api');
app.use('/api', api);
app.use('/users', users);
app.use('/:name', function(req, res, next) {
console.log('from app.js name:', req.params.name);
res.render('index', {
title: req.params.name
});
}, routes);
app.use('/', function(req, res, next) {
res.render('index', {
title: 'MainPage'
});
});
If a request come from client localhost:3000/api/abc and localhost:3000/user/abc, the response from api and user module. But if I make a request like localhost:3000/myName/xyz, the app module return a response. This behavior let me concern about what is precedence of expressjs and what is correct order for router modules. Why routers do not confuse between actions "api", "users" and parameter ":name". Please let me understand clearly how express does and what is precedence.
The order is first come first serve.
In your case, if user hits /api, he will get response from api, but if you write /:name route before /api, /:name will serve for /api requests also.
Case1: /api will serve requests for /api.
var routes = require('./routes/index');
var users = require('./routes/users');
var api = require('./routes/api');
app.use('/api', api);
app.use('/users', users);
app.use('/:name', function(req, res, next) {
console.log('from app.js name:', req.params.name);
res.render('index', {
title: req.params.name
});
}, routes);
app.use('/', function(req, res, next) {
res.render('index', {
title: 'MainPage'
});
});
Case2: /:name serves requests for /api and /users
var routes = require('./routes/index');
var users = require('./routes/users');
var api = require('./routes/api');
app.use('/:name', function(req, res, next) {
console.log('from app.js name:', req.params.name);
res.render('index', {
title: req.params.name
});
}, routes);
app.use('/api', api);
app.use('/users', users);
app.use('/', function(req, res, next) {
res.render('index', {
title: 'MainPage'
});
});
The example given in the ExpressJS documentation is pretty simple and was unfortunately difficult to find. I only found it through another SO question.
Middleware functions are executed sequentially, therefore the order of middleware inclusion is important
app.use(function(req, res, next) {
res.send('This is the only thing that ever happens')
}
app.use(...) // never used
app.get(...) // never used
app.put(...) // never used
app.post(...) // never used
I am trying to make az ExtJS application with a Node.js server. My server code currently looks like this:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendfile(filedir + '/index.html');
});
app.get('/employees', function(req, res){
console.log("hello");
});
app.listen(3000);
When I open localhost:3000 in a browser, the html file is load, but not correctly. Checking in firebug I see that itt cannot find the linked files in html. For example
"NetworkError: 404 Not Found - http://localhost:3000/ext-4/ext-debug.js".
This is quite logical, since the file doesn't exist on that URL. My question would be how to fix this issue, so it could find every single linked file on my filesystem.
I'm clearly doing something wrong or missing something, I am totally new in node.
Doesn't look like you're configuring Express' static file handler.
Try adding this code:
app.configure(function() {
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
app.use(express.logger("short"));
});
It would go right after var app = ... like this:
var express = require('express');
var app = express();
app.configure(function() {
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
app.use(express.logger("short"));
});
app.get('/', function (req, res) {
res.sendfile(filedir + '/index.html');
});
app.get('/employees', function(req, res){
console.log("hello");
});
app.listen(3000);
And then place your static files under the ./public directory.
You'll want to use some static middleware such as: http://www.senchalabs.org/connect/static.html
note express inherits connect so you can
app.use(express.static(filedir));
Or the full thing:
var express = require('express');
var app = express();
app.use(express.static(filedir));
app.get('/employees', function(req, res){
console.log("hello");
res.send("hello");
});
app.listen(3000);