Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vkarpov15
Last active October 26, 2017 22:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkarpov15/e03dafb2ac478cb38ff3fbe4c36139d6 to your computer and use it in GitHub Desktop.
Save vkarpov15/e03dafb2ac478cb38ff3fbe4c36139d6 to your computer and use it in GitHub Desktop.
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