Consistent Arrays from Query Params with Express and Archetype
const Archetype = require('archetype'); | |
const express = require('express'); | |
const superagent = require('superagent'); | |
const app = express(); | |
const Params = new Archetype({ | |
names: { $type: ['string'], $default: [] } | |
}).compile('Params'); | |
app.get('/', function(req, res) { | |
res.json(new Params(req.query)); | |
}); | |
run().catch(error => console.error(error)); | |
async function run() { | |
await app.listen(3000); | |
const root = 'http://localhost:3000'; | |
// {"names":[]} | |
console.log(await superagent.get(`${root}`).then(res => res.text)); | |
// {"names":["test"]} | |
console.log(await superagent.get(`${root}?names=test`).then(res => res.text)); | |
// {"names":["batman","robin"]} | |
console.log(await superagent.get(`${root}?names=batman&names=robin`).then(res => res.text)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment