Create Node js-express API from Scratch

Samraj Soundarajan
3 min readJul 7, 2021

Node JS is a runtime environment which executes JavaScript

Express is a minimal web application framework. Node and express combined together provides powerful and simple backend services.

Lets start building your first nodejs-express-api project. I am using vscode editor for this project in mac OS, you can use any editor which you are comfortable with.

Step 1: Open vs-code and Open terminal in it.

Step 2: Now create a folder for your project => mkdir node-express api

Step 3: If you have installed node skip this step, npm is node package management and npx is node package execute

  • To run npm command you need node in your machine, Downloadable link to install node or if you want to use package manage to install node.
  • check versions of node and npm or to see node installed successfully in your machine by using node -v and npm -v, it will display node version and npm version installed.

Step 4: To Initialise your node application

  • npm init -y (Initialising node with default settings) Recommended
  • npm init (Initialising node with user input settings)

Step 5: To install express

  • npx express-generator (Command creates express template for your project)

Above is the folder structure created for your project.

  • Public Folder -> For creating files to access throughout the application.
  • Routes Folder -> For creating route javascript files to navigate based on url, e.g. http://localhost:3000/users -> this url directs the api to access users.js file
  • Views Folder -> Created to display a UI on the browser, in simple terms, making the response from api to a readable format in UI.
  • app.js is base file of your application
  • bin Folder -> Server Connection file (Do not delete or touch until its necessary to change your server configurations)

Step 6: Before running the application, express-generator included all the required libraries in your package.json file. To generate the modules locally to support your server to under the purpose of the libraries, we need to install all the packages locally.

  • npm install or npm i (this installs are the library modules mentioned in package.json)
  • Refresh your project folder, you would be seeing a folder node_modules contains the library files.

Step 7: We have completed the setup for this project, to view the project on your browser

  • npm start (triggers the start script in your package.json)
  • Go to browser and type http://localhost:3000/ and you should be seeing the below image.

You have successfully setup your node-express-api. It should be the same for windows os as well.

  • Editor: vscode
  • Operating System: macos
  • Framework: express

Thank you.

--

--

Samraj Soundarajan

Full Stack Developer, Freelancer, Application advisor, AWS expert, React/Native, Javascript.