DEV Community

Cover image for Say HELLO to Nodejs and Expressjs
Sakshi
Sakshi

Posted on • Updated on

Say HELLO to Nodejs and Expressjs

HEY Everyone!
Getting more patriotic today :P

This is my first project tutorial, where we will be dipping toes into the water of Nodejs and Expressjs

So lets begin this awesome work, hope you are as thrilled as I am.

Prerequisites -

  • I am using VS Code as my IDE, hope you use atom or VS code
  • You should know basics of Javascript
  • You should know what are the extensions of JS and html files
  • You should be familiar with command line or git commands (Okay, its optional)

So what is this nodejs?

Nodejs is a JS framework.
It is used to work on backend with JS
It is superfast and allows to create scalable web applications.
Netflix, Twitter, Uber etc use Nodejs in backend

Oh come on!! Lets not drill into its theory, lets see what we can do with Nodejs

Step 1 - Working with Nodejs (Everything in command line, I am using git bash)

  • Download nodeJS and install it

  • Check NodeJS version

node --version

  • pwd - to know path of current directory

pwd

  • Make new directory

mkdir Nodedemo

  • Go into this directory

cd Nodedemo

  • Create files from command line (Not related to this project)

touch index.js

  • Just NODE things

When we install node we also install REPL

Type 'node' in command line and hit enter

Now write your code here, an arrow appears in the next line
Write your JS code here

To exit, type '.exit'

Lets start making , create a file 'server.js'

  • In the command line type
npm install express
Enter fullscreen mode Exit fullscreen mode
  • On the top of server.js, write
//jshint esversion6
Enter fullscreen mode Exit fullscreen mode
  • Below it, type
const express = require("express");
Enter fullscreen mode Exit fullscreen mode

A const variable named express now requires "express", variable name need not to be express always

  • Type
const app = express();
Enter fullscreen mode Exit fullscreen mode

This function represents the express module

  • Now choose a port here, from where it listens request
app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

3000 is my choice, you can use 5000, 8000 any number you want

  • In command line type
node server.js
Enter fullscreen mode Exit fullscreen mode

and make sure you are in the same folder where this server.js exists, in the command line

Press Ctrl+C to exit the server in command line

  • Now as you cant see anything in cmd, make listen function to listen requests your server gets

Type this

app.listen(3000,function(){
console.log("Server started at post 3000");
});

Enter fullscreen mode Exit fullscreen mode

Now check in browser, type

localhost:3000 in URL box and press ENTER

  • You will se 'CANNOT GET/' in the browser

  • Its time to handle request and response now
    Your server is listening on port 3000 but can not get any requests and cant respond

  • Add app.get method

app.get("/",function(request,response){
response.send("hello world");

});
Enter fullscreen mode Exit fullscreen mode

Now the server works
Remember everytime you add something new, refresh the server

Now you will see "hello world" in the browser

Thanks for reading

Top comments (4)

Collapse
 
iravshan profile image
Sadikov Dev

thank you

Collapse
 
iravshan profile image
Sadikov Dev

yeah

Collapse
 
iravshan profile image
Sadikov Dev

oh yeah

Collapse
 
yongchanghe profile image
Yongchang He

Nice tutorial thank you!