How to upload, display and save images using node.js and express

First of all, you should make an HTML form containing a file input element. You also need to set the form’s enctype attribute to multipart/form-data: <form method=”post” enctype=”multipart/form-data” action=”/upload”> <input type=”file” name=”file”> <input type=”submit” value=”Submit”> </form> Assuming the form is defined in index.html stored in a directory named public relative to where your script is … Read more

What does passport.session() middleware do?

passport.session() acts as a middleware to alter the req object and change the ‘user’ value that is currently the session id (from the client cookie) into the true deserialized user object. Whilst the other answers make some good points I thought that some more specific detail could be provided. app.use(passport.session()); is equivalent to app.use(passport.authenticate(‘session’)); Where … Read more

res.sendFile absolute path

The express.static middleware is separate from res.sendFile, so initializing it with an absolute path to your public directory won’t do anything to res.sendFile. You need to use an absolute path directly with res.sendFile. There are two simple ways to do it: res.sendFile(path.join(__dirname, ‘../public’, ‘index1.html’)); res.sendFile(‘index1.html’, { root: path.join(__dirname, ‘../public’) }); Note: __dirname returns the directory … Read more

Basic HTTP authentication with Node and Express 4

Simple Basic Auth with vanilla JavaScript (ES6) app.use((req, res, next) => { // ———————————————————————– // authentication middleware const auth = {login: ‘yourlogin’, password: ‘yourpassword’} // change this // parse login and password from headers const b64auth = (req.headers.authorization || ”).split(‘ ‘)[1] || ” const [login, password] = Buffer.from(b64auth, ‘base64’).toString().split(‘:’) // Verify login and password are … Read more

How to set custom favicon in Express?

In Express 4 Install the favicon middleware and then do: var favicon = require(‘serve-favicon’); app.use(favicon(__dirname + ‘/public/images/favicon.ico’)); Or better, using the path module: app.use(favicon(path.join(__dirname,’public’,’images’,’favicon.ico’))); (note that this solution will work in express 3 apps as well) In Express 3 According to the API, .favicon accepts a location parameter: app.use(express.favicon(“public/images/favicon.ico”)); Most of the time, you might … Read more

How to access a preexisting collection with Mongoose?

Mongoose added the ability to specify the collection name under the schema, or as the third argument when declaring the model. Otherwise it will use the pluralized version given by the name you map to the model. Try something like the following, either schema-mapped: new Schema({ url: String, text: String, id: Number}, { collection : … Read more

NodeJS: How to get the server’s port?

Express 4.x answer: Express 4.x (per Tien Do’s answer below), now treats app.listen() as an asynchronous operation, so listener.address() will only return data inside of app.listen()’s callback: var app = require(‘express’)(); var listener = app.listen(8888, function(){ console.log(‘Listening on port ‘ + listener.address().port); //Listening on port 8888 }); Express 3 answer: I think you are looking … Read more

Node.js Error: Cannot find module express

You need to install Express locally into the context of your application (node_modules folder): $ npm install express The reason for this is that applications always look in their local context for any dependencies. The global installation is only for setting up system-wide available binaries, such as unit test runners or bootstrappers or things like … Read more