Quick Introduction to Express + Node.Js in under 30 Seconds if you know Rails

Express + Node.Js is extremely simple to start serving things with. At times, I even had to ask myself if I was even doing it right. Hopefully this is post is just as simple to understand.

I do want to say a disclaimer that this isn’t your final version since you’ll be adding folders likemodels and controllers, but its mostly to get you past the Node + Express intro hurdle. Another disclaimer, Node + Express is much more relatable to Sinatra then Rails.

Make sure you have the following installed.

$ npm i -g node express nodemon 

We’ll create a directory for our project and them run the following. I use atom, but if you use another text editor besides for atom you can replace

$ atom . with the text editor of your choice.

$ mkdir node-express-intro
$ cd !$
$ atom .
$ npm init -y
$ npm i -S express
$ touch server.js

Copy the following code into your server.js file

var express = require('express')
var app = express()
var port = 3000
app.set('port', port)
app.get('/', function(req, res){
res.send('Express works!')
})
app.listen(port, function(){
console.log(
`Express is running on $(prot) press Ctrl-C to terminate`
)
})

Now run the following to your terminal :

$ nodemon server.js

If you go to your browser and type http://localhost:3000 you should get the message Express works! on your browser screen.

Here’s an amazing link that got me started on my path of Node + Express https://www.youtube.com/watch?v=L5Nle1VXYnw&t=649s.