websitelkak.blogg.se

How to install nvm on windows 10 using cmd
How to install nvm on windows 10 using cmd






And that's why frameworks like ExpressJS, HapiJS exist, which makes creating servers in NodeJS very easy. With an increasing number of routes, such things become hard to manage as we will have to add multiple if statements. We saw how we had to handle each request with its method and the URL. With that, we have created a simple node server to serve static content as well a simple API to return JSON data.

  • We have also added an else statement to handle all other requests where we simply return a message to the browser that the request is not supported.
  • We return a notes array in the JSON format, and we are setting the Content-Type header to be of application/json for the browser to process it accordingly.
  • Here, we have added one more if statement to handle the request coming at /notes.
  • createServer ( serverHandler ) // callback function definition function serverHandler ( req, res ) =>. Server.js const http = require ( 'http' ) const path = require ( 'path' ) const fs = require ( 'fs' ) const PORT = 3000 const hostname = 'localhost' // createServer is the http method used to create a web server that takes a callback.

    how to install nvm on windows 10 using cmd

    Now we will create a node server that will serve our index.html file when a request is sent to /. Index.html Demo Coming from the Root Step 3: Add our server code to serve the static assets Create a directory called public at the root of your project.Īdd a file named index.html with the following content.

    how to install nvm on windows 10 using cmd

    Create a file called server.js in your directory.By this time, you will have a package.json file in your directory.Step 1: Initialize the projectĬreate a folder by any name and initialize a node project in it using npm init -y In the process, you will also learn about the request and response objects provided by NodeJS, which are used by every other framework built on top of NodeJS like ExpressJS etc. We will create a simple server that will serve static assets and a simple API to return some JSON data. In this blog, I will show you how to create a NodeJS server using core http module.








    How to install nvm on windows 10 using cmd