"use strict"; var express = require('express'), fs = require('fs'), app = express(), customers = JSON.parse(fs.readFileSync('data/customers.json', 'utf-8')), states = JSON.parse(fs.readFileSync('data/states.json', 'utf-8')), inContainer = process.env.CONTAINER, inAzure = process.env.WEBSITE_RESOURCE_GROUP, port = process.env.PORT || 8080; app.use(express.urlencoded({ extended: true })); app.use(express.json()); //CORS app.use((req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Credentials", true); res.header("Access-Control-Allow-Headers", "Origin, Authorization, X-Requested-With, X-XSRF-TOKEN, X-InlineCount, Content-Type, Accept"); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS'); next(); }); //The dist folder has our static resources (index.html, css, images) if (!inContainer) { app.use(express.static(__dirname + '/dist')); console.log(__dirname); } app.get('/api/customers/page/:skip/:top', (req, res) => { const topVal = req.params.top, skipVal = req.params.skip, skip = (isNaN(skipVal)) ? 0 : +skipVal; let top = (isNaN(topVal)) ? 10 : skip + (+topVal); if (top > customers.length) { top = skip + (customers.length - skip); } console.log(`Skip: ${skip} Top: ${top}`); var pagedCustomers = customers.slice(skip, top); res.setHeader('X-InlineCount', customers.length); res.json(pagedCustomers); }); app.get('/api/customers', (req, res) => { res.json(customers); }); app.get('/api/customers/:id', (req, res) => { let customerId = +req.params.id; let selectedCustomer = null; for (let customer of customers) { if (customer.id === customerId) { // found customer to create one to send selectedCustomer = {}; selectedCustomer = customer; break; } } res.json(selectedCustomer); }); app.post('/api/customers', (req, res) => { let postedCustomer = req.body; let maxId = Math.max.apply(Math,customers.map((cust) => cust.id)); postedCustomer.id = ++maxId; postedCustomer.gender = (postedCustomer.id % 2 === 0) ? 'female' : 'male'; customers.push(postedCustomer); res.json(postedCustomer); }); app.put('/api/customers/:id', (req, res) => { let putCustomer = req.body; let id = +req.params.id; let status = false; //Ensure state name is in sync with state abbreviation const filteredStates = states.filter((state) => state.abbreviation === putCustomer.state.abbreviation); if (filteredStates && filteredStates.length) { putCustomer.state.name = filteredStates[0].name; console.log('Updated putCustomer state to ' + putCustomer.state.name); } for (let i=0,len=customers.length;i { res.json(states); }); app.post('/api/auth/login', (req, res) => { var userLogin = req.body; //Add "real" auth here. Simulating it by returning a simple boolean. res.json(true); }); app.post('/api/auth/logout', (req, res) => { res.json(true); }); if (!inContainer) { // redirect all others to the index (HTML5 history) app.all('/*', function(req, res) { res.sendFile(__dirname + '/dist/index.html'); }); } app.listen(port); console.log('Express listening on port ' + port); // //Open browser if (!inContainer && !inAzure) { var opn = require('opn'); opn('http://localhost:' + port).then(() => { console.log('Browser closed.'); }); }