Episode #21— Starting a nodeJS project from scratch

Stephens Xu
Fullstack Network
Published in
3 min readJun 29, 2017

--

Mat, one of our regular viewer on twitch, have asked Avi to do a streaming session on how to start a nodeJS project from scratch, without using too much pre-made package. Mat ended up joining this session on Skype and we ended up putting together a simplistic sample nodeJS starter app along with the basic installation commands. We thought this would be helpful for other people too since node is becoming so popular and I’m also trying to learn it myself.

Here is the open sourced repo Sample NodeJS starter app.

This starter app is based on Express. Express is one of the most popular minimalist web framework for NodeJS, similar with Sinatra in the Ruby land. The basic libraries we use with express are yarn(faster package management), babel-cli and express morgan(better logging). We also needed to add a few packages for EC5

npm install -g yarn
npm install -g babel-cli
yarn add express morgan
yarn add --dev babel-cli babel-preset-es2015

Then we need to create a .babelrc in root directory and put the following content

{
"presets": ["es2015"]
}

The very first step would be creating a package.json file:

npm init -y

And this is what our package.json look like:

{
"name": "sample.nodejs.api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "rm -rf dist/ && babel ./src --out-dir ./dist/ --ignore ./node_modules,./.babelrc./.package.json --copy-files",
"start": "npm run build && node ./dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.3",
"morgan": "^1.8.2",
"pg": "^6.4.0",
"pg-hstore": "^2.3.2",
"sequelize": "^4.2.0"
},
"devDependencies": {
"babel-preset-es2015": "^6.24.1",
"sequelize-cli": "^2.7.0"
}
}

Notice the two shell scripts under "scripts" are our custom written script that cleans up the directories and runs index.js. Both build and start will be executed when we do npm start

Now we have the plumbing, we need some basic structure in our index.js

import express from "express";

const app = express();

routes(app);

app.use("/", (req, res) => {
res.send("Hello world");
});

app.listen(3000, () => {
console.log("Listening on port 3000");
});

Here we ask Express to listen on port 3000 and define the action when we send get request to the root / director. In this case we simply return “Hello world”.

Now do

npm start

Navigate to http://localhost:3000/

There! Now we have a bare bone node application running on our local machine!

Later on we also connected a postgres DB with our app:

yarn add sequelize pg pg-hstore

And added a few more routes in our routs.js

import { createUser, getAllUsers } from "./controllers/users"

export default function routes(app) {
app.get("/users", getAllUsers);
app.post("/users", createUser);
}

So that we can list, create and store users objects in local database.

For more details see code here!

Join us in our streaming on twitch if you’re interested for more!

--

--