Calling Express Route internally from inside NodeJS

The ‘usual’ or ‘correct’ way to handle this would be to have the function you want to call broken out by itself, detached from any route definitions. Perhaps in its own module, but not necessarily. Then just call it wherever you need it. Like so: function updateSomething(thing) { return myDb.save(thing); } // elsewhere: router.put(‘/api/update/something/:withParam’, function(req, … Read more

What does “res.render” do, and what does the html file look like?

What does res.render do and what does the html file look like? res.render() function compiles your template (please don’t use ejs), inserts locals there, and creates html output out of those two things. Answering Edit 2 part. // here you set that all templates are located in `/views` directory app.set(‘views’, __dirname + ‘/views’); // here … Read more

Multiple View paths on Node.js + Express

Last Update The multiple view folders feature is supported by the framework since Express 4.10 Just pass an array of locations to the views property, like so. app.set(‘views’, [__dirname + ‘/viewsFolder1’, __dirname + ‘/viewsFolder2′]); Express 2.0 As far as I know express doesn’t support multiple view paths or namespaces at the moment (like the static … Read more

Stream files in node/express to client

You can stream directly to the response object (it is a Stream). A basic file stream would look something like this. function(req, res, next) { if(req.url===”somethingorAnother”) { res.setHeader(“content-type”, “some/type”); fs.createReadStream(“./toSomeFile”).pipe(res); } else { next(); // not our concern, pass along to next middleware function } } This will take care of binding to data and … Read more

How do I stream response in express?

You don’t need a readable stream instance, just use res.write(): res.write(“USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD\n”); for (var i = 0; i < 10; i++) { res.write(“23,John Doe,1234,500,SUBSCRIPITON,100,ACTIVE,30\n”); } res.end(); This works because in Express, res is based on Node’s own http.serverResponse, so it inherits all its methods (like write).