Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?

Try this out! axios.get(‘your_url’, {withCredentials: true}); //for GET axios.post(‘your_url’, data, {withCredentials: true}); //for POST axios.put(‘your_url’, data, {withCredentials: true}); //for PUT axios.delete(‘your_url’, data, {withCredentials: true}); //for DELETE For more information on this from the axios docs: “withCredentials indicates whether or not cross-site Access-Control requests should be made using credentials” – https://github.com/axios/axios More detail on withCredentials: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials

Express.js sendfile() vs. render()

The render method works when you have a templating engine, such as Handlebars or Jade, in use. A templating engine is something that parses a given template file and generates HTML output. This is so you can generate an HTML web page depending on some variables in your program. Such templates are often used with … Read more

Express Passport (node.js) error handling

The strategy-implementation works in conjunction with passport.authenticate to both authenticate a request, and handle success/failure. Say you’re using this route (which is passed an e-mail address and a password): app.post(‘/login’, passport.authenticate(‘local’, { successRedirect: ‘/loggedin’, failureRedirect: ‘/login’, // see text failureFlash: true // optional, see text as well }); This will call the code in the … Read more

Is it possible to set a base URL for NodeJS app?

The express router can handle this since 4.0 http://expressjs.com/en/api.html#router http://bulkan-evcimen.com/using_express_router_instead_of_express_namespace.html var express = require(‘express’); var app = express(); var router = express.Router(); // simple logger for this router’s requests // all requests to this router will first hit this middleware router.use(function(req, res, next) { console.log(‘%s %s %s’, req.method, req.url, req.path); next(); }); // this will … Read more

How to get rid of Connect 3.0 deprecation alert?

This is a warning that will go away once Express updates to use Connect 3.0 – as a temporary fix, follow the instructions at the top of https://github.com/senchalabs/connect/wiki/Connect-3.0. Specifically, find this line in your app: app.use(express.bodyParser()); And replace it with the following (this is what bodyParser will be in 3.0): app.use(express.json()); app.use(express.urlencoded());

How to store a file with file extension with multer?

I have a workaround for the adding proper extension of files. If you use path node module var multer = require(‘multer’); var path = require(‘path’) var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, ‘uploads/’) }, filename: function (req, file, cb) { cb(null, Date.now() + path.extname(file.originalname)) //Appending extension } }) var upload = … Read more

Node.js + express.js + passport.js : stay authenticated between server restart

There’s an opensource called connect-mongo that does exactly what you need – persists the session data in mongodb usage example (with a reuse of mongoose opened connection) : var session = require(‘express-session’); var MongoStore = require(‘connect-mongo’)(session); var mongoose = require(‘mongoose’); mongoose.connect(‘mongodb://localhost/sess’); app.use(express.session({ secret:’secret’, maxAge: new Date(Date.now() + 3600000), store: new MongoStore( // Following lines of … Read more