NodeJS accessing file with relative path [duplicate]

You can use the path module to join the path of the directory in which helper1.js lives to the relative path of foobar.json. This will give you the absolute path to foobar.json. var fs = require(‘fs’); var path = require(‘path’); var jsonPath = path.join(__dirname, ‘..’, ‘config’, ‘dev’, ‘foobar.json’); var jsonString = fs.readFileSync(jsonPath, ‘utf8’); This should … Read more

passport.js passport.initialize() middleware not in use

Follow the example to avoid the out-of-order middleware hell that express makes it so easy to enter. Straight from the docs. Note how yours does not match this exactly. var app = express(); app.use(require(‘serve-static’)(__dirname + ‘/../../public’)); app.use(require(‘cookie-parser’)()); app.use(require(‘body-parser’).urlencoded({ extended: true })); app.use(require(‘express-session’)({ secret: ‘keyboard cat’, resave: true, saveUninitialized: true })); app.use(passport.initialize()); app.use(passport.session()); Docs cookieParser session … Read more

Node.js – logging / Use morgan and winston

This article does an excellent job for what you want to do. http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ For your specific code you probably need something like this: var logger = new winston.Logger({ transports: [ new winston.transports.File({ level: ‘info’, filename: ‘./logs/all-logs.log’, handleExceptions: true, json: true, maxsize: 5242880, //5MB maxFiles: 5, colorize: false }), new winston.transports.Console({ level: ‘debug’, handleExceptions: true, json: … Read more

Node/Express file upload

ExpressJS Issue: Most of the middleware is removed from express 4. check out: http://www.github.com/senchalabs/connect#middleware For multipart middleware like busboy, busboy-connect, formidable, flow, parted is needed. This example works using connect-busboy middleware. create /img and /public folders. Use the folder structure: \server.js \img\”where stuff is uploaded to” \public\index.html SERVER.JS var express = require(‘express’); //Express Web Server … Read more

How do I get the domain originating the request in express.js?

You have to retrieve it from the HOST header. var host = req.get(‘host’); It is optional with HTTP 1.0, but required by 1.1. And, the app can always impose a requirement of its own. If this is for supporting cross-origin requests, you would instead use the Origin header. var origin = req.get(‘origin’); Note that some … Read more

How to use Morgan logger?

Seems you too are confused with the same thing as I was, the reason I stumbled upon this question. I think we associate logging with manual logging as we would do in Java with log4j (if you know java) where we instantiate a Logger and say log ‘this’. Then I dug in morgan code, turns … Read more