Working on the front-end using HTML, CSS & Javascript and wondering how RESTful API works in the backend. Then you can start working on it using Nodejs.
What is Nodejs?
Node.js is a server-side programming framework built on Chrome's JavaScript engine, which provides event-driven and non-blocking I/O. The coding is done in Javascript, and its latest version is v11.9.0. Read the docs for further information.
Nodejs installation guide:
- Download the Nodejs application from the official Node.js official site: https://nodejs.org/en/download/ Follow the steps depending on your OS.
- After installation, check for the version of Node and NPM using the command in your terminal NPM is the Node Package Manager and is a tool for installing, uninstalling and maintaining package modules for your app. NPM is installed along with Node:
node -v
npm -v
After verifying the versions of both Node and NPM, now create a new folder for our server.
Run the below commands in your terminal to create a new folder, and inside a folder, create one new file, index.js.
$ mkdir SampleServer
$ cd SampleServer
SampleServer$ touch index.js
After successfully creating folders and files, run the following code:
SampleServer$ npm init -y
Running the command npm will create a new file package.json.
package.json - It holds various metadata/dependencies relevant to the project. This file gives information to npm, which allows it to identify the project and handle the project's dependencies.
After configuring Node and npm, now it's time to install express. ExpressJS is a web framework for Node that provides you with a simple API to build websites, web apps and backends. With ExpressJS, you don't need to worry about low-level protocols, processes, etc. In order to install express, run the following command to add the framework.
SampleServer$ npm install --save express
This will add all the dependency of express in the server application.
Now it's finally time to write some codes. Add the following code to your index.js file.
var express = require('express')
const app = express()
app.get('/', res, req => {
res.send("Hello world!")
})
app.listen(3000, function() {
console.log('App running in port 3000');
});
A variable name 'express' is holding a new express application.
app.listen(3000) implies that the server is listening local in port number 3000. Now run the server by using the following command.
SampleServer$ node index.js
Now request the server which you've just created from the browser.
You successfully created a sample server.
Few important express methods to be familiar with:
- app.listen(): To listen to the server in a particular port.
- app.get(): The GET method requests data from the server. Requests using the GET method should only retrieve data and should have no other effect.
- app.post(): The POST method requests the server to accept data that is enclosed in the request as a new object/entity of the resource identified by the URI.
- app.put(): The PUT method requests the server to accept the data enclosed in the request as a modification to existing data identified by the URI. In case data does not exist, then the PUT method should create one.
- app.delete(): It is a delete method for express. The DELETE method requests the server to remove/delete the specified resource.
Did you enjoy this? Please give it a like so others can discover it as well.
You may also like my other articles:
- Console.log and other console methods to debug your code in JavaScript
- Things to keep in mind before starting Javascript framework
- var, let, and const – Why to avoid var 😷 and how to put the other two to good use? - Javascript
If you liked the article, feel free to share it to help others find it!
You may also follow me on LinkedIn and Twitter.
💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter here.
Top comments (0)