Setting Up A Node + Express Backend

Node is Javascript that runs on the V8 engine that executes code outside of a web browser on the backend. Node is useful to know because you can have more direct control of your backend as well as it can keep your entire project uniform by only using Javascript and Javascript frameworks. Express is a Node.js framework used to create web applications and APIs. Below I will go over how to setup a Node.js and Express backend.

First, you want to do some installations. Make your node project folder and then, in your terminal, run:

npm init -y
npm i express cors
npm i -g nodemon

Make an index.js file. This will be your main node file.

In your package.json file in scripts, you want to add “start” : “ node index.js” and “dev” : “nodemon index.js”

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},

In your index. js file, you want to require express, cors, and set up your port.

const express = require('express');
const app = express();
const cors = require('cors');
const { response } = require('express');
const PORT = 4000;

That’s all for setting up your Node.js + Express backend! In your terminal, run npm start in your node project to run the server. In this case, after starting your server, you would go to http://localhost:4000 to go to your node server since you setup your port to be on 4000. I will explain how to setup Knex if you plan to use a relational database such as postgreSQL with node in your backend in a future blogpost.