Send HTTP POST request in node.js

var http = require(‘http’);

var body = ‘hello world’;

var options = {

    hostname: ‘your host ip’,

    port: 80,

    path: ‘/’,

    method: ‘POST’,

    headers: {

        ‘Content-Length’: Buffer.byteLength(body)



var req = http.request(options, function(res) {

    console.log(‘STATUS: ‘ + res.statusCode);

    console.log(‘HEADERS: ‘ + JSON.stringify(res.headers));


    res.on(‘data’, function (chunk) {


        console.log(‘BODY: ‘ + chunk);




req.on(‘error’, function(e) {


    console.log(‘problem with request: ‘ + e.message);



// write data to request body




Don’t forgot the header “’Content-Length’: Buffer.byteLength(body)”, this is important!

FAMO.US UNIVERSITY University is an excellent way to start. University includes live coding lessons, tutorials, and discrete examples tied to our reference documentation. In University, each lesson lets you live code in and immediately see the rendered results without installing anything. Learn, play, and code to see what it’s like to be

The code you see in the code editor is creating the slideshow app in the preview window. If we change the code, the preview window will rebuild live, as long as there are no compile errors.

There are two methods to build in create everything from basic components or piece together pre-assembled widgets. Let’s start with the most basic component – the Surface.

Adding an assembled widget in can be just as simple as adding a surface. Like we did when we created our surface, we specify some options when we instantiate our object. Then to display our assembled widget, we add it to the main context.

Node.js best practices

1. Learn the best practices of JavaScript first:

Not joking! Before starting Node.js you should learn the best practices of JavaScript first. It will make your code more decent and flexible. There is a statement code lovers always use, “A fool can write codes which only machines can understand”. So, follow the guidelines to make your code understandable to others.


2. Remember Node.js is Asynchronous:

Asynchronous is one of the biggest advantage for Node.js, but I’m sure more than 60% developers faced problem for its asynchronous property, and I’m sure too that everybody knows the solution. When you are running a time consuming job like accessing database or forEach function use counter to keep track of the loop, else before completing the loop the next function will start.

To help resolve this problem, I suggest you to consider some libraries such as q or async. it can help you avoid callback hell.


3. Style Guide

  • Use 2 spaces for indenting your code and swear an oath to never mix tabs and spaces – a special kind of hell is awaiting you otherwise.
  • 80 characters per line. Limit your lines to 80 characters. Yes, screens have gotten much bigger over the last few years, but your brain has not. Use the additional room for split screen, your editor supports that, right?
  • Opening braces go on the same line.
  • Declare one variable per var statement
  • Use lowerCamelCase for variables, properties and function names.
  • Use UpperCamelCase for class names
  • Use UPPERCASE for Constants
  • Use Semicolons.
  • Use single quotes, unless you are writing JSON.


4. Write small functions

Keep your functions short. A good function fits on a slide that the people in the last row of a big room can comfortably read. So don’t count on them having perfect vision and limit yourself to ~15 lines of code per function.


5. Return early from functions

To avoid deep nesting of if-statements, always return a function’s value as early as possible. e.g.

function isPercentage(val) {
  if (val < 0) {
    return false;

  if (val > 100) {
    return false;

  return true;

6. Use npm to manage your dependencies

  • Start new projects with npm init
  • Declare all dependencies
  • Use npm scripts to run custom build steps

npm is like a blessing to the Node.js developers. Speaking of managing library dependencies, the Node Package Manager deserves to be called out. Node Package Manager is the root of almost all deployment systems for Node.js. It underlies the many PaaS (Platform-as-a-Service) providers for Node.js. Its simple, dependable package management has let the Node ecosystem grow extremely well in recent time, to the point that the underlying public service now needs to scale to the next level. 62,961 npms are there till date; they can help you to do interesting stuff and keeps your code simple


7. Do not extend built-in prototypes

This is also a javascript best practice.


8. Node.js require(s) best practices

let’s talk about some best practices when using the require() function.

a) Have a consistent order when requiring modules.

  • core modules come first
  • public modules from npm / node_modules folder come second
  • Your own modules come at the end

b) Give variable names that match the module.

c) Include .json in filename. e.g.  var data = require('./data.json');

d) Do not override core modules


9. Handle errors properly:

This point is very useful for the beginners. You should keep in mind that your node can crash for one unhandled error. So, my suggestion is to handle all the error properly, and throws them with proper message. Use of try-catch block will slow your process. So, try to avoid it. And one more thing, using process.on(‘uncaughtException’) is a bad habit. It will protect your node from crashing, but the error will remain unhandled.


10. Function structure:

  • Error should be the first parameter.
  • Callback should be the last parameter.
  • Be sure to use ‘return’ statement.


11. Use library functions

Sometimes, you need to manipulate time, array, object and collections because of app requirement. It will be helpful for you to use Underscore.js, moment.js. They are a very interesting and useful tools.


12. Unit-Testing

Writing unit-tests for asynchronous code is hard, but with some testing frameworks and libs, it quite convenience. You can try Mocha, jasmine, suppertest, istanbul, should.js etc

javascript, jQuery tips

1. Convert a json object into a pretty format string

You do not need library to do that, here is an example

var obj = {a:1, 'b':'foo']};
var str = JSON.stringify(obj, undefined, 2); // indentation level = 2

The str will be with pritty format string

“a”: 1,
“b”: “foo”

2. Get the closest parent form of an element

$(this).closest('form').submit(); // for submit button

Web development via angularjs, javascript,, c#, html5, SEO, etc