NodeJS w/Express Error: Cannot GET /

You typically want to render templates like this: app.get(“https://stackoverflow.com/”, function(req, res){ res.render(‘index.ejs’); }); However you can also deliver static content – to do so use: app.use(express.static(__dirname + ‘/public’)); Now everything in the /public directory of your project will be delivered as static content at the root of your site e.g. if you place default.htm in … Read more

Building enterprise app with Node/Express

Controllers are God objects until you don’t want them to be so… *   – you don’t say zurfyx (╯°□°)╯︵ ┻━┻* Just interested in the solution? Jump onto the latest section “Result”. ┬──┬◡ノ(° -°ノ) Prior getting started with the answer, let me apologize for making this response way longer than the usual SO length. Controllers alone … Read more

Express.js get http method in controller

The answer was quite easy exports.register = function(req, res) { if (req.method == “POST”) { // do form handling } res.render(‘user/registration.html.swig’, { form: form.toHTML() }); }; But I searched a long time for this approach in the express guide. Finally the node documentation has such detailed information: http://nodejs.org/api/http.html#http_http_request_options_callback

Best way to store DB config in Node.Js / Express app

Here’s how I do it: Create a config.js which contains objects representing your configs: var config = { development: { //url to be used in link generation url: ‘http://my.site.com’, //mongodb connection settings database: { host: ‘127.0.0.1’, port: ‘27017’, db: ‘site_dev’ }, //server details server: { host: ‘127.0.0.1’, port: ‘3422’ } }, production: { //url to … Read more

How to modify the nodejs request default timeout time?

I’m assuming you’re using express, given the logs you have in your question. The key is to set the timeout property on server (the following sets the timeout to one second, use whatever value you want): var server = app.listen(app.get(‘port’), function() { debug(‘Express server listening on port ‘ + server.address().port); }); server.timeout = 1000; If … Read more

What is the smartest way to handle robots.txt in Express?

Use a middleware function. This way the robots.txt will be handled before any session, cookieParser, etc: app.use(‘/robots.txt’, function (req, res, next) { res.type(‘text/plain’) res.send(“User-agent: *\nDisallow: /”); }); With express 4 app.get now gets handled in the order it appears so you can just use that: app.get(‘/robots.txt’, function (req, res) { res.type(‘text/plain’); res.send(“User-agent: *\nDisallow: /”); });

Node.js + Express.js User Permission Security Model

Having it per-route usually works for me. This is what I typically do: function requireRole (role) { return function (req, res, next) { if (req.session.user && req.session.user.role === role) { next(); } else { res.send(403); } } } app.get(“/foo”, foo.index); app.get(“/foo/:id”, requireRole(“user”), foo.show); app.post(“/foo”, requireRole(“admin”), foo.create); // All bars are protected app.all(“/foo/bar”, requireRole(“admin”)); // All … Read more