Last active
October 26, 2017 22:55
-
-
Save vkarpov15/e03dafb2ac478cb38ff3fbe4c36139d6 to your computer and use it in GitHub Desktop.
Consistent Arrays from Query Params with Express and Archetype
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 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