An easy way to start a NodeJs server without a framework.

An easy way to start a NodeJs server without a framework.

I will be explaining everything in the code below, immediately after the code snippets, so you have nothing to fear as I use to🤭.

Any sentences in the code below, written immediately after //, are called comments, which are used to describe the function of a code.

Prerequisites

All you need to get started will be listed below.

Steps to start the server

  1. Create a folder on your local machine.
  2. Create a new file inside the newly created folder. The file name will be index.js for this article.
  3. Add the code snippets below to the newly created file.
  4. Open the terminal.
  5. Run node index.js in the terminal.
  6. Go to "localhost:4000" in the browser to see the message Hello world in the body of the browser.
//Dependencies

const http = require('http')

//Initialize the server

const server = http.createServer((request,response) => {
    //Get the response
    response.end('Hello world');
})

//Start the server and let it listen on port 4000

server.listen(4000 () => {
    console.log('The server is listening on port 4000')
})

Brief explanation of the code above

  • HTTP: This is NodeJs built-in module, which enables the communication between the server, and the client. This can also be said to sending of requests and responses over the server.

  • CreateServer: This is a built-in callback method in HTTP, it triggers your computer to become a server, listens to the port assigned to it, and renders the response expected to the user.

  • request: This is a parameter passed as the first argument in the createServer() method. The request has a lot of data passed to it, so we just make use of the data we need. I will show you below what a request looks like after being logged. Add the snippet below immediately after the //Get the response

console.log(request)

If you follow the steps correctly you should see something like this

 <ref *2> IncomingMessage {
  _readableState: ReadableState {
    objectMode: false,
    highWaterMark: 16384,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    pipes: [],
    flowing: null,
    ended: false,
    endEmitted: false,
    reading: false,
    constructed: true,
    sync: true,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: false,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: true,
    destroyed: false,
    errored: null,
    closed: false,
    closeEmitted: false,
    defaultEncoding: 'utf8',
    awaitDrainWriters: null,
    multiAwaitDrain: false,
    readingMore: true,
    dataEmitted: false,
    decoder: null,
    encoding: null,
    [Symbol(kPaused)]: null
  },
  _events: [Object: null prototype] { end: [Function: clearRequestTimeout] },
  _eventsCount: 1,
  _maxListeners: undefined,
  • response: The response also comes with built-in data from sending a response on an HTTP server, it is similar to what we have above.

  • response.end: This brings an end to the response sent from the server. This tells the server to bring an end to the response, stating that all responses needed have been received. The output of the code below will appear on the body of the browser by going to "localhost:4000" on the browser.

response.end('Hello world')
//You should see "Hello World"
  • server. listen: This makes the server look for the particular port which was passed to it. In the example above, we made use of port 4000.

Conclusion

NodeJs is a backend language, which executes javascript code, outside of a browser. This article is the bedrock for becoming a proficient backend engineer. With this guide, starting a nodejs server should be much easier to fix. With this article, starting a nodejs server without no dependency and framework will be easy to go about.