What is the difference between post api call and form submission with post method?

There are multiple ways to submit a form from the browser: HTML form, submit button, user presses submit button, no Javascript involved. HTML form in the page, Javascript gets DOM element for the form and calls .submit() method on the form object. Ajax call using the XMLHttpRequest interface with the POST method and manually sending … Read more

How to downgrade to a previous Node version

Warning: This answer does not support Windows OS You can use n for node’s version management. There is a simple intro for n. $ npm install -g n $ n 6.10.3 this is very easy to use. then you can show your node version: $ node -v v6.10.3 For windows nvm is a well-received tool.

Node.js returning result from MySQL query

You have to do the processing on the results from the db query on a callback only. Just like. function getColour(username, roomCount, callback) { connection.query(‘SELECT hexcode FROM colours WHERE precedence = ?’, [roomCount], function(err, result) { if (err) callback(err,null); else callback(null,result[0].hexcode); }); } //call Fn for db query with callback getColour(“yourname”,4, function(err,data){ if (err) { … Read more

In Node.js / Express, how do I “download” a page and gets its HTML?

var util = require(“util”), http = require(“http”); var options = { host: “www.google.com”, port: 80, path: “https://stackoverflow.com/” }; var content = “”; var req = http.request(options, function(res) { res.setEncoding(“utf8”); res.on(“data”, function (chunk) { content += chunk; }); res.on(“end”, function () { util.log(content); }); }); req.end();

socket.io private message

No tutorial needed. The Socket.IO FAQ is pretty straightforward on this one: socket.emit(‘news’, { hello: ‘world’ }); EDIT: Folks are linking to this question when asking about how to get that socket object later. There is no need to. When a new client connects, save a reference to that socket on whatever object you’re keeping … Read more

How to chain a variable number of promises in Q, in order?

There’s a nice clean way to to this with [].reduce. var chain = itemsToProcess.reduce(function (previous, item) { return previous.then(function (previousValue) { // do what you want with previous value // return your async operation return Q.delay(100); }) }, Q.resolve(/* set the first “previousValue” here */)); chain.then(function (lastResult) { // … }); reduce iterates through the … Read more