creating a node.js server

creating a node.js server

you'll be creating a server using express.js, which is a backend web application framework for Node.js. With express you can connect your web app to a database and many other powerful things involving backend.

Now let's get to work, you need to make sure you have Node.js installed you can check setting up node on how to do this, Now you'll have to create a folder where all your files will be, we'll be using NPM to install packages(JavaScript libraries), this are what an application needs either for development, testing or production of an application.

Using node, Express is another package to be installed, dependencies like express and others that you may need will be managed in a file called package.json, to get this file you'll simply type 'npm init' in the terminal.

npm init

The first thing that'll show up is the name you want to give your package, make sure you fill that, the version, description, entry point, test command, git repository, keywords, author which is you and make sure you fill it, license, and asking if everything you set is okay you should just click enter for them all. And now your package.json file should look like this.

{
  "name": "create-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Mukhtar",
  "license": "ISC"
}

Now create your index.js file which is the main file, you'll need to install Express.js with NPM as a dependency.

npm install express

Now your dependencies section in the package.json file will include Express.js.

{
  "name": "create-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Mukhtar",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

To use the Express library, you need the require function in your index.js file this way.

const express = require('express');

And then another variable and assign it to the express function, this way you are creating an express application, And a port where you want your app to always listen to.

const app = express()
const PORT = 5000

Now you want to send a text to the home screen which is ' Hello world ' any time the browser loads the root path '/', you'll be using the app.get() method which takes two parameters: the path and a callback function, request(req) and response(res) are the parameters the callback function takes.

app.get('/', (req, res) =>{
    res.send('Hello world')
})

Now to make your server listen to the PORT that you defined, the app.listen() method is what you'll use, this will take two parameters: the PORT and an optional callback, this callback is useful because it makes you know the server has started already.

app.listen(PORT, () =>{
    console.log(`Server is listening at port ${PORT}`)
});

To start your server, type this in your terminal. Note: index.js is our main file in the package.json.

node index.js

Type http://localhost:5000/ in your browser and you should see your web app's response, Wow you've come this far, Thank you for reading.