Skip to content

express js

This is a super quick way to make a backend in javascript. It looks a lot like flask, but this is nodejs and not python.

Mostly from :

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website

installation

install express
1
2
3
4
5
npm install express
npm install express-generator -g
express helloworld
cd helloworld
npm install

The express generator allows you to create a skeleton app for further development. You can start the demo with :

start
npm start

You will be able to see the demo hello app on http://localhost:3000/ But this will have to be restarted every time you make a change. To have it reload on file changes, you need to install nodemon :

install nodemon
1
2
3
4
# globally
sudo npm install -g nodemon
# or locally, note that you need this one for the npm devstart further down
npm install --save-dev nodemon

nodemon is a 'monitoring node'. If you run npm start it will internally look in package.json and run the "scripts" section there. In the skeleton application it reads :

package.json
1
2
3
"scripts": {
    "start": "node ./bin/www"
},

So you could also run this with the same result.

run
node ./bin/www

Or.. the newly installed nodemon, which watches file changes :

live server
1
2
3
4
5
6
7
8
9
node ./bin/www

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node bin/www`
# ok lets try :
rs
[nodemon] starting `node bin/www`

Of course we want to just run npm start and you can add other script entries for instance for development :

package.json
1
2
3
4
"scripts": {
    "start": "node ./bin/www",
    "devstart" : "nodemon ./bin/www"
},

Now you can choose to run in development/monitoring mode or not

run devstart
1
2
3
4
npm run start
npm run devstart
npm start # runs 'start'
npm devstart # fails !!