How to print object in Node JS

Basic console.log will not go through long and complex object, and may decide to just print [Object] instead. A good way to prevent that in node.js is to use util.inspect: ‘use strict’; const util = require(‘util’), obj = /*Long and complex object*/; console.log(util.inspect(obj, {depth: null})); //depth: null tell util.inspect to open everything until it get … 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

Reading binary data in node.js

The ‘binary’ encoding is an alias for ‘latin1′, which you clearly don’t want when reading non-character data. If you want the raw data, don’t specify an encoding at all (or supply null)*. You’ll get a Buffer instead of a string, which you’d then want to use directly rather than using toString on it. * (Some … Read more

Difference of using async / await vs promises?

async/await and promises are closely related. async functions return promises, and await is syntactic sugar for waiting for a promise to be resolved. The only drawback from having a mix of promises and async functions might be readability and maintainability of the code, but you can certainly use the return value of async functions as … Read more

Injecting CSS into site with Puppeteer

addStyleTag: You can use page.addStyleTag to add some style which will either add a link or style tag based on your options which can be a url, path or some css content. // url await page.addStyleTag({url: ‘http://example.com/style.css’}) // path, can be relative or absolute path await page.addStyleTag({path: ‘style.css’}) // content await page.addStyleTag({content: ‘.body{background: red}’}) evaluateOnNewDocument: … Read more

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