Created
December 22, 2019 14:47
-
-
Save kephin/1356c8c1ebc9805c4510ea73b2b828e1 to your computer and use it in GitHub Desktop.
params/query/data in server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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