Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
Last active March 7, 2020 10:00
Show Gist options
  • Save YonatanKra/c6baa8f8e46e55192d2a6f256ac4fc15 to your computer and use it in GitHub Desktop.
Save YonatanKra/c6baa8f8e46e55192d2a6f256ac4fc15 to your computer and use it in GitHub Desktop.
// grab the packages we need
const express = require('express');
const app = express();
const faker = require('faker');
const port = process.env.PORT || 3000;
const nResponses = 1000;
const TYPES = {
WORKER: 'WORKER',
COP: 'COP',
TEACHER: 'TEACHER'
};
TYPES.getRandom = function() {
const keys = Object.keys(this);
return this[keys[Math.floor(Math.random() * (keys.length - 1))]];
}
const TYPE_TOOLS = {
WORKER: ['hammer', 'nail', 'ladder', 'helmet', 'screwdriver', 'swiss knife', 'boots'],
COP: ['uniform', 'taser', 'pistol', 'police hat', 'badge', 'whistle'],
TEACHER: ['marker', 'book', 'notebook', 'tablet', 'papers', 'tie']
};
class UserMetaData {
constructor(type) {
this.type = type;
this.tools = TYPE_TOOLS[type];
}
}
class User {
constructor(index, type) {
this.id = `${index}-${new Date().getTime()}`;
this.name = faker.name.firstName() + ' ' +faker.name.lastName();
this.email = faker.internet.email();
this.metaData = new UserMetaData(type);
}
}
class FlyWeightUser {
constructor(index, type) {
this.id = `${index}-${new Date().getTime()}`;
this.name = faker.name.firstName() + ' ' +faker.name.lastName();
this.email = faker.internet.email();
this.type = type;
}
}
function generateData(userClass) {
let users = [];
for (let i = 0; i < nResponses; i++) {
users.push(new userClass(i, TYPES.getRandom()));
}
return users;
}
// routes will go here
app.post('/getData', function(req, res) {
res.send(generateData(User));
});
app.post('/getFlyWeightData', function(req, res) {
res.send({data: generateData(FlyWeightUser), TYPE_TOOLS});
});
app.use(express.static('dist'));
// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment