Skip to content

Instantly share code, notes, and snippets.

@guiliredu
Last active March 13, 2020 21:09
Show Gist options
  • Save guiliredu/dd1bf1df60869fe6f62843e07f7602a2 to your computer and use it in GitHub Desktop.
Save guiliredu/dd1bf1df60869fe6f62843e07f7602a2 to your computer and use it in GitHub Desktop.
Express - Node Backend Studies

Express.js

Installing

$ npm install express --save

Hello World

// index.js
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Than run $ node index.js

Express generator

https://expressjs.com/en/starter/generator.html

Installing Express generator: $ npm install -g express-generator Creating a new project: $ express myapp && cd myapp && npm install

Routing

https://expressjs.com/en/guide/routing.html

// Basic routing
app.get('/', function (req, res) {
  res.send('Hello World!')
})

// Parameters
app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})
// Route path: /users/:userId/books/:bookId
// Request URL: http://localhost:3000/users/34/books/8989
// req.params: { "userId": "34", "bookId": "8989" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment