Node.js and Express (+nodemon)
$ mkdir express-server
$ cd express-server/
$ yarn init -y
$ yarn add express
$ yarn install
create a server.js file and copy and paste↓
from this website
http://expressjs.com/en/starter/hello-world.html
server.js
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () =>
console.log('Example app listening on port 3000!'));
$ yarn add nodemon --env
$ node server.js
command + T ⇨ with running server,
$ curl localhost:3000
Hello World!
Go to http://localhost:3000/
It should show...
Hello World!
post & get
Edit like this
server.js
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('This is a GET\n');
});
app.post('/', (req, res) => {
res.send('This is a POST\n');
});
app.listen(3000, () =>
console.log('Example app listening on port 3000!'));
Then run
$ curl -X POST localhost:3000
This is a POST
And open it in browse http://localhost:3000/
It should show ⇨ This is a GET