Web Dev Bootcamp ∙ Introduction to Node and Express JS
Hieu Nguyen · May 07, 2020 · 3 min read
0
0 leave some love!
Goals
- Explain what NodeJS is
- Create a server using NodeJS
What is NodeJS
NodeJS is a server runtime that allows you to run javascript code on the server.
Prior to NodeJS, all Javascript code had to be run in the browser using the V8 engine provided by the browser
Javascript became so popular that ideas of writing Javascript without a browser began circulating.
In response, a group of very capable people designed NodeJS to enable developer to write JavaScript on the server.
They also open sourced the technology, so new features and bug fixes are added every day
Nodejs Libraries
NodeJS is a low level framework that allows you to create a server. Hence, there are libraries that sits on top of NodeJS that makes sever management much easier
There are many libraries to choose from, such as Express JS, Hapi, SailJS, and Meteor JS.
In this course, we will be using Express JS
What is Express?
Fast, unopinionated, minimalist web framework for Node.js - expressjs.com
Express is simply a wrapper for NodeJS that makes developing in Node Js super easy
Installing Node
Window - use the installer
Mac - use brew to install Node and NPM
brew update
brew install node
Linux - Install Node PPA and then isntall Node using terminal
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
At the time of writing this the LTS version was 12.x.
Then install node by running
sudo apt install nodejs
check if node and npm were installed successfully
node -v
npm -v
Creating Our First Server
- create a new project folder
mkdir firstNodeServer
cd firstNodeServer
- Add a package.json by running
npn init
there will be a series of questions about the project. Feel free to use the defaults
The package.json is a json file that contains a list of packages required to build our project
- Installing the Express package
The Express package is the npm package that we need to create a node server using the Express JS framework
npm install express
- Create a new index.js file
touch index.js
This file will contain our server initialization code to create a new server
- Inside the index.js, add the following code
var express = require("express")
var app = express()
const PORT = 3000
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`)
})
Here we are importing the Express package that we just installed using var express = require('express')
.
Then we are creating a new app using express by calling var app = express()
Next we have a const PORT
that will tell our server to listen on a specific port
Finally we are telling our app to listen on PORT = 3000 and console.log a message when the app is listening on that port
- Start our server
There are two main ways of starting our server
- Using standard node command. However, each time you make a change to your files, you have to restart the server
node index.js
- Using nodemon - a small npm package that allows you to hot-reload your server each time a change is detected
install by running:
npm install -g nodemon
here the -g
flag is telling npm to install nodemon globally => meaning we can run nodemon in any directory!
Run our server again by using
nodemon index.js
try changing the message in the console.log() to something else. Nodemon will automatically restart the server for you!
What’s Next?
Understand request routing and building an API