How do I detect whether I am on server on client in next.js?
Now (2020 Jan) it should be typeof window === ‘undefined’ since process.browser is deprecated Refer to https://github.com/zeit/next.js/issues/5354#issuecomment-520305040
Now (2020 Jan) it should be typeof window === ‘undefined’ since process.browser is deprecated Refer to https://github.com/zeit/next.js/issues/5354#issuecomment-520305040
Edit 2: Release 1.15.2 of the body parser module introduces raw mode, which returns the body as a Buffer. By default, it also automatically handles deflate and gzip decompression. Example usage: var bodyParser = require(‘body-parser’); app.use(bodyParser.raw(options)); app.get(path, function(req, res) { // req.body is a Buffer object }); By default, the options object has the following … Read more
Update To completely remove debugging use: var io = require(‘socket.io’).listen(app, { log: false }); Where app is node.js http server / express etc. You forgot to mention you are also using socket.io. This is coming from socket.io. You can disable this by configuration: io.set(‘log level’, 1); // reduce logging
It is actually very easy to do this using the “set” and “get” methods available on an express object. Example as follows, say you have a variable called config with your configuration related stuff that you want to be available in other places: In app.js: var config = require(‘./config’); app.configure(function() { … app.set(‘config’, config); … … Read more
You can change the default behavior at the schema definition level using the select attribute of the field: password: { type: String, select: false } Then you can pull it in as needed in find and populate calls via field selection as ‘+password’. For example: Users.findOne({_id: id}).select(‘+password’).exec(…);
router.get is only for defining subpaths. Consider this example: var router = express.Router(); app.use(‘/first’, router); // Mount the router as middleware at path /first router.get(‘/sud’, smaller); router.get(‘/user’, bigger); If you open /first/sud, then the smaller function will get called. If you open /first/user, then the bigger function will get called. In short, app.use(‘/first’, router) mounts … Read more
I’m not sure what you want to happen when a user accesses /login or /, but you can create separate routes for those; if you declare them before the catch-all, they get first dibs at handling the incoming requests: app.get(‘/login’, function(req, res) { … }); app.get(“https://stackoverflow.com/”, function(req, res) { … }); app.get(‘*’, function(req, res) { … Read more
The value 1382086394000 is probably a time value, which is the number of milliseconds since 1970-01-01T00:00:00Z. You can use it to create an ECMAScript Date object using the Date constructor: var d = new Date(1382086394000); How you convert that into something readable is up to you. Simply sending it to output should call the internal … Read more
As others have recommended in comments, it looks like the canonical way to test Express controllers is through supertest. An example test might look like this: describe(‘GET /users’, function(){ it(‘respond with json’, function(done){ request(app) .get(“https://stackoverflow.com/users”) .set(‘Accept’, ‘application/json’) .expect(200) .end(function(err, res){ if (err) return done(err); done() }); }) }); Upside: you can test your entire stack … Read more
The following works if you have something like this in your app.js: http.createServer(app).listen(app.get(‘port’), function(){ console.log(“Express server listening on port ” + app.get(‘port’)); }); Either explicitly hardcode your code to use the port you want, like: app.set(‘port’, process.env.PORT || 3000); This code means set your port to the environment variable PORT or if that is undefined … Read more