Setting up two different static directories in node.js Express framework

You can also set the path that static files will be served to the web from by specifying an additional (first) parameter to use() like so: app.use(“/public”, express.static(__dirname + “/public”)); app.use(“/public2”, express.static(__dirname + “/public2”)); That way you get two different directories on the web that mirror your local directories, not one url path that fails … Read more

Best way to perform a full text search in MongoDB and Mongoose

You can add a text index to your Mongoose schema definition that lets you use the $text operator in your find queries to search all fields included in the text index. To create an index to support text search on, say, name and profile.something: var schema = new Schema({ name: String, email: String, profile: { … Read more

Redirecting to previous page after authentication in node.js using passport.js

In your ensureAuthenticated method save the return url in the session like this: … req.session.returnTo = req.originalUrl; res.redirect(‘/login’); … Then you can update your passport.authenticate route to something like: app.get(‘/auth/google/return’, passport.authenticate(‘google’), function(req, res) { res.redirect(req.session.returnTo || “https://stackoverflow.com/”); delete req.session.returnTo; });

How to send a custom http status message in node / express?

None of the existing answers accomplish what the OP originally asked for, which is to override the default Reason-Phrase (the text appearing immediately after the status code) sent by Express. What you want is res.statusMessage. This is not part of Express, it’s a property of the underlying http.Response object in Node.js 0.11+. You can use … Read more

req.query and req.param in ExpressJS

req.query will return a JS object after the query string is parsed. /user?name=tom&age=55 – req.query would yield {name:”tom”, age: “55”} req.params will return parameters in the matched route. If your route is /user/:id and you make a request to /user/5 – req.params would yield {id: “5”} req.param is a function that peels parameters out of … Read more

how to properly close node-express server?

app.listen() returns http.Server. You should invoke close() on that instance and not on app instance. Ex. app.get( ‘/auth/github/callback’, passport.authenticate(‘github’, { failureRedirect: ‘/login’ }), function(req, res) { res.redirect(“https://stackoverflow.com/”); setTimeout(function () { server.close(); // ^^^^^^^^^^^ }, 3000) } ); var server = app.listen(‘http://localhost:5000/’); // ^^^^^^^^^^ You can inspect sources: /node_modules/express/lib/application.js

How to find the size of the file in Node.js?

To get a file’s size in megabytes: var fs = require(“fs”); // Load the filesystem module var stats = fs.statSync(“myfile.txt”) var fileSizeInBytes = stats.size; // Convert the file size to megabytes (optional) var fileSizeInMegabytes = fileSizeInBytes / (1024*1024); or in bytes: function getFilesizeInBytes(filename) { var stats = fs.statSync(filename); var fileSizeInBytes = stats.size; return fileSizeInBytes; }