[Node.js] Put some code into our node application !!


In our previous blog, we have created our brand new node application. now we will put some code into our node sample application now.

if you list your node application parent directory, you can find only package.json listed. Now we will create index.js file in our project.

Now our current directory will look like :

 nodeapp
    ├───package.json
    └───index.js

Put console.log("hello world") in our index.js file. Now try to start our application using following command.

npm start

oops!! it throws error stating missing script: start


 nodeapp
    ├───package.json
    ├───index.js
    └───npm-debug.log


☝☝Question: Hey, I see new file created namenpm-debug.log in my application directory? why it is created ? how it is created?
It is created by npm. If there is any error with npm command execution, npm-debug.log file will be created you can find exact error details in that file. Above scenario missing script is the cause for the creation of log file.

☝☝Question: Now, How I can fix this error?
Nothing to worry, We just need to add an entry in our package.json file.


Current Package.json

{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "My node app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Modified Package.json

{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "My node app",
  "main": "index.js",
  "scripts": {
    "start":"node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

After modification, try npm start,πŸ‘πŸ‘πŸ‘ Bingo our application runs successfully!

πŸ’¬πŸ’¬πŸ’¬πŸ’¬ oops still we have used console.log() only!!

Let's jump deep into node:

copy the following code into index.js and start our application using npm start command.


const http = require('http');
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
  res.end('Hello World\n');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});



output:
Server running at http://localhost:3000/


πŸ‘πŸ‘πŸ‘Bingo!! our Application is running!!

☝☝Question: how to check whether my application is running?
Very simple!! Open your browser just hit http://localhost:3000/. Able to see hello world in browser. You have successfully ran your application. 

Code Explanation:

const http = require('http');

Above statement is something similar to importing package in java. we are including 'http' package into our index.js. http is an inbuilt module of node JS so you can directly include it into your application.

☝☝ Question: How i can able to identify the inbuilt modules of node JS?
It is very simple, Just check the node api documentation (https://nodejs.org/dist/latest-v6.x/docs/api/). You can find all the available modules in node.

const hostname = 'localhost';
const port = 3000;

Above two statement is just the variable declaration. You can specify any port but make sure port is available in your machine.

If port is not available then application will throw ECONNREFUSED error. so make sure port is available in your machine.

const server = http.createServer((req, res) => {
  res.end('Hello World\n');
});

createserver() method is present in the http module. When the url is hit from the server it will invoke this method and in response we are sending "Hello World" as response.

There are many inbuilt modules available in the Node Js, we will look into some of the modules in the upcoming blogs. In the next blog we will look into usage of external node modules 😁😁😁.

Please share your comments or questions in the below comment section!!πŸ‘πŸ‘

Comments