Where is the body in a nodejs http.get response?

http.request docs contains example how to receive body of the response through handling data event: var options = { host: ‘www.google.com’, port: 80, path: ‘/upload’, method: ‘POST’ }; var req = http.request(options, function(res) { console.log(‘STATUS: ‘ + res.statusCode); console.log(‘HEADERS: ‘ + JSON.stringify(res.headers)); res.setEncoding(‘utf8’); res.on(‘data’, function (chunk) { console.log(‘BODY: ‘ + chunk); }); }); req.on(‘error’, function(e) … Read more

Is there a way to get the version from the ‘package.json’ file in Node.js code?

I found that the following code fragment worked best for me. Since it uses require to load the package.json, it works regardless of the current working directory. var pjson = require(‘./package.json’); console.log(pjson.version); A warning, courtesy of @Pathogen: Doing this with Browserify has security implications. Be careful not to expose your package.json to the client, as … Read more

Upgrading Node.js to the latest version

Ubuntu Linux/Mac The module n makes version-management easy: npm install n -g For the latest stable version: n stable For the latest version: n latest Debian 10 Upgrade older versions of node and npm on Debian 10 as follows: sudo su -c ‘curl -sL https://deb.nodesource.com/setup_18.x | bash -‘ sudo apt-get install nodejs -y sudo apt … Read more

Payload error in jsonwebtoken

It fails at the line const token = jwt.sign(user, config.secret, { With error “Expected “payload” to be a plain object” Your user object is initialized here: User.getUserByUsername(username, (err, user) Which I assume is mongoosejs object, which contains many methods and is not “serializable”. You could handle this by passing a plain object, by either using … Read more