Skip to content

Instantly share code, notes, and snippets.

@kephin
Created December 22, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kephin/1356c8c1ebc9805c4510ea73b2b828e1 to your computer and use it in GitHub Desktop.
Save kephin/1356c8c1ebc9805c4510ea73b2b828e1 to your computer and use it in GitHub Desktop.
params/query/data in server
const express = require('express')
const app = express()
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
app.get('/:greet', (req, res) => {
// curl http://localhost:8080/hello\?name\=kevin
console.log('====================================')
console.log('req.params :', req.params) // { greet: 'hello' }
console.log('req.query :', req.query) // { name: 'kevin' }
console.log('====================================')
})
app.post('/:greet', (req, res) => {
// curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" http://localhost:8080/hello\?name\=kevin
console.log('====================================')
console.log('req.params :', req.params) // { greet: 'hello' }
console.log('req.query :', req.query) // { name: 'kevin' }
console.log('req.body :', req.body) // { key1: 'value1', key2: 'value2' }
console.log('====================================')
})
const server = app.listen(8080, () => console.log('listening at 8080...'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment