Check node version mac2/14/2024 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. 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.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.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |