Node 7.1.0 new Promise() resolver undefined is not a function

The API for promises requires you to pass a function to the promise constructor. Quoting MDN: new Promise( /* executor */ function(resolve, reject) { … } ); executor – A function that is passed with the arguments resolve and reject. The executor function is executed immediately by the Promise implementation, passing resolve and reject functions … Read more

Mongoose sort the aggregated result

The results returned from the aggregation pipeline are just plain objects. So you do the sorting as a pipeline stage, not as a separate operation: Recommend.aggregate( [ // Grouping pipeline { “$group”: { “_id”: ‘$roomId’, “recommendCount”: { “$sum”: 1 } }}, // Sorting pipeline { “$sort”: { “recommendCount”: -1 } }, // Optionally limit results … Read more

Restrict access to Node.js-based HTTP server by IP address

I’m not sure how bulletproof is this approach, but here it is, collected from answers around the web: var http = require(‘http’); http.createServer(function (req, res) { var ip = req.ip || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress; if (ip == ‘127.0.0.1’) // exit if it’s a particular ip res.end(); … Please, someone more proficient in node … Read more

Typeorm when trying to run migrations: Missing required argument: dataSource

TypeOrm removed ormconfig.json support in version 0.3.0. You should use new syntax – create ormconfig.ts and specify options for you database, for example: export const connectionSource = new DataSource({ migrationsTableName: ‘migrations’, type: ‘postgres’, host: ‘localhost’, port: 5432, username: ‘user’, password: ‘pass’, database: ‘somehealthchecker’, logging: false, synchronize: false, name: ‘default’, entities: [‘src/**/**.entity{.ts,.js}’], migrations: [‘src/migrations/**/*{.ts,.js}’], subscribers: [‘src/subscriber/**/*{.ts,.js}’], … Read more

Express – Return binary data from webservice

Here is my slightly cleaned up version of how to return binary files with Express. I assume that the data is in an object that can be declared as binary and has a length: exports.download = function (data, filename, mimetype, res) { res.writeHead(200, { ‘Content-Type’: mimetype, ‘Content-disposition’: ‘attachment;filename=” + filename, “Content-Length’: data.length }); res.end(Buffer.from(data, ‘binary’)); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)