Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 31, 2022 10:58 am GMT

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
  • On the top of server.js, write
//jshint esversion6
  • Below it, type
const express = require("express");

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

  • Type
const app = express();

This function represents the express module

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

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

  • In command line type
node server.js

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 might get an errorThe solution to get rid from it is to make listen function

Type this

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

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");});

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


Original Link: https://dev.to/bellatrix/say-hello-to-nodejs-and-expressjs-j62

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To