DEV Community

Cover image for Setup a Node/ExpressJS API Project in less than 10mins
Abdul-Samii Ajala
Abdul-Samii Ajala

Posted on • Edited on

Setup a Node/ExpressJS API Project in less than 10mins

Create your project repository

Create a repo on Github for your new Node project. Give it a name &description.

  • Choose whether you want the repo to be private or public.
  • Add .gitignore and select Node

Create a repo

  • Upon creating your repository copy the ssh link of the project

..
..

Clone the repo

In your terminal, navigate to your project folder and run

git clone <repo_link>
Enter fullscreen mode Exit fullscreen mode

this will clone the files from your Github repo to your laptop

  • cd into the newly cloned project and run
npm init
Enter fullscreen mode Exit fullscreen mode
  • Answer all the questions accordingly, that should create a file named package.json for you.
  • In your terminal, install your dev dependencies; eslint, babel & nodemon
npm i -D eslint nodemon @babel/node @babel/cli @babel/core @babel/plugin-transform-async-to-generator @babel/plugin-transform-runtime @babel/preset-env eslint-config-prettier
Enter fullscreen mode Exit fullscreen mode

set project configurations

  • create babel.config.json
{
  "presets": ["@babel/preset-env"],
  "plugins": ["@babel/plugin-transform-runtime"]
}
Enter fullscreen mode Exit fullscreen mode

In your project directory, create src/index.js file and create a simple server in it like so

import express from 'express'
const app = express()

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

app.listen(9090, () => {
    console.log('Server is running on port 9090')
})
Enter fullscreen mode Exit fullscreen mode
  • In your package.json, let's add some scripts
{
    ...
    "script": {
      "dev": "nodemon --exec babel-node src",
      "start": "node dist/",
      "lint": "./node_modules/.bin/eslint . --fix",
      "build": "npm run lint && babel src -d dist"
    },
    ...
}
Enter fullscreen mode Exit fullscreen mode

Install express as a dependency

npm i express
Enter fullscreen mode Exit fullscreen mode

Be sure to initialize your eslint like so;

npx eslint --init
Enter fullscreen mode Exit fullscreen mode
  • choose To check syntax, find problems, and enforce code style
  • select JavaScript modules (import/export)
  • under which framework, select none of these
  • where does your code run? Check Node only
  • How would you like to define a style for your project? select Use a popular style guide
  • choose Standard: [https://github.com/standard/standard](https://github.com/standard/standard)
  • What format do you want your config file to be in? select JSON
  • Would you like to install them now with npm? Choose Yes suggestions above can be modified based on your specific project need

The series of actions above will generate an eslint configuration file.

  • Edit the configuration file. Under the extends field, add "prettier" like so;
{
  ...
  "extends": [
    ...
    "prettier"
  ],
  ...
}
Enter fullscreen mode Exit fullscreen mode
  • Create .eslintignore file and exclude dist folder like so
dist/**
Enter fullscreen mode Exit fullscreen mode

Finally, let's create nodemon.json

{
  "verbose": false,
  "delay": "0",
  "ignore": ["*.test.js", "dist/*"]
}
Enter fullscreen mode Exit fullscreen mode

Run your code

npm run dev
Enter fullscreen mode Exit fullscreen mode

You should get a message in the console that reads

Server is running on port 9090

Commit and push your work to Github

💪🏽👨🏽‍💻🚀

Cover image credit: Clément H, Unsplash.com

Top comments (0)