Skip to content

Node.js Application Setup

We need to open our console, and naviagte to our todo_project folder we created at the beggining. There, besides client-app folder we want to create new folder called server-app where we will initiate our Node.js application.

After you create the folder you can naviage to it through Console or VS Code Terminal and execute command

npm init

You will be asked to answer some question so npm can create your project. Value between () is the default value, if you click Enter the value will remain, otherwise you can enter your value and click proceed.

You can answer them as we did below:

package name: (server-app)
version: (1.0.0)
description: Sample Todo List server app
entry point: (index.js)
test command:
git repository:
author: Your Name
license: (ISC)

After you enter the last value it will basically ask you if you are sure you want to save entered values.

About to write to /Users/<your-username>/server-app/package.json:

  "name": "server-app",
  "version": "1.0.0",
  "description": "Sample Todo List server app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "Your Name",
  "license": "ISC"

Is this OK? (yes) yes
Click Enter to confirm and your package.json file should be generated inside server-app folder. Since we defined our "entry point" as "index.js", let's create that file inside server-app folder and leave it empty for now.

Now, we want to install and setup Express.js framework for Node, but we first need to install it by executing the following command inside server-app folder

npm install --save express

After we install Express, we want to setup simple express application inside our index.js.

const express = require('express');
const app = express();
const port = 3001;

app.get('/', (req, res) => {
  res.send('Hello World!');

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
Nice, our simple Node application is ready to run. In order to run it, make sure your Terminal is inside server-app folder and execute command node index.js

After you run it successfully you should see this message in your terminal Example app listening at http://localhost:3001

And if you visit http://localhost:3001 in your browser you should see "Hello World!" message.

That means that you initialized your Node + Express application successfully, congrats!

We need to make sure our client application will be allowed to send data without restrictions.

For this part we will need to apply CORS rules using cors library.

What is CORS?

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.

To install it just execute

npm install --save cors

Also, lets update our index file to apply CORS settings.

const express = require('express');
const cors = require('cors');
const app = express();
const port = 3001;


app.use(cors({origin: '*'}));

app.get('/', (req, res) => {
  res.send('Hello World!');

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);

As you can see, we setup our server to be able to recieve requests from any (*) origin. You can also define just specific origin or many origins.

Last update: March 28, 2022