How to create a basic  Node.js and express API

How to create a basic Node.js and express API

unnamed.jpg In this article, I will demonstrate how to create a basic Node.js application that serves content to the client in JSON.

First, make sure you have Node.js installed on your computer. see link below to install Node.js

Create a folder and name it (Name of your choice)

Open command line or Terminal and move to the new folder directory just created.

Run the following code

npm init

Follow the steps as below: (Note: change your entry point to server.js)

frame1.PNG

Open Folder in any code editor of your choice. I use vsCode. Open package.json file and add this code snippet in scripts below test put a comma and add

"start": "node server.js"

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "This is a test Server",
  "main": "server.js",

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "Pascal Ojinnaka",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Installing Express

Open folder directory on your command prompt or Terminal and type this command

npm install express

Create a new file server.js import express and initialize with the code below

const express = require('express')

const app = express()

const PORT = 5000

app.listen(PORT, 
    console.log(`Server running on port ${PORT}`))

Save and run the application with

npm start

when you get "Server running on port 5000" on your console, congratulations your application is working.

Launch this URL on your browser

http://localhost:5000

Create new file product.js and add the following snippet

const products = [
  {
    id: 1,
    name: 'Airpods Wireless Bluetooth Headphones',
    image: '/images/airpods.jpg',
    description:
      'Bluetooth technology lets you connect it with compatible devices wirelessly High-quality AAC audio offers immersive listening experience Built-in microphone allows you to take calls while working',
    brand: 'Apple',
    category: 'Electronics',
    price: 89.99,
    countInStock: 10,
    rating: 4.5,
    numReviews: 12,
  },
  {
    id: 2,
    name: 'iPhone 11 Pro 256GB Memory',
    image: '/images/phone.jpg',
    description:
      'Introducing the iPhone 11 Pro. A transformative triple-camera system that adds tons of capability without complexity. An unprecedented leap in battery life',
    brand: 'Apple',
    category: 'Electronics',
    price: 599.99,
    countInStock: 4,
    rating: 4.0,
    numReviews: 8,
  },
  {
    id: 3,
    name: 'Cannon EOS 80D DSLR Camera',
    image: '/images/camera.jpg',
    description:
      'Characterized by versatile imaging specs, the Canon EOS 80D further clarifies itself using a pair of robust focusing systems and an intuitive design',
    brand: 'Cannon',
    category: 'Electronics',
    price: 929.99,
    countInStock: 5,
    rating: 3,
    numReviews: 12,
  }
module.exports = products

Open server.js file to import products file. At the top import products.js

const products = require('./products')
const express = require('express')
const products = require('./products')

const app = express()


const PORT = 5000

app.listen(PORT, 
    console.log(`Server running on port ${PORT}`))

app.get('/', (req, res) => {
    res.send('API is running')
})

app.get('/api/products', (req, res) => {
    res.json(products)
})

Stop your application with CTRL+C and RUN with

npm start

Enter the following endpoint on your browser

http://localhost:5000/api/products

Create your git repository and push to github account (Add node_modules in .gitignore file)

Congratulations you have created a Node.Js and Express API