Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Server side for issuance client scripts
- */
- import path from "path";
- import Express from 'express';
- import BodyParser from "body-parser";
- import { readFile, writeFile } from "./util";
- const root = process.cwd();
- const p = path.resolve(root, 'build');
- const configFile = path.resolve(root, 'config/config.json');
- const app = Express();
- app.use(Express.static(p));
- app.use(BodyParser.urlencoded({ extended: false }));
- app.use(BodyParser.json());
- app.get('/', (req, res) => {
- res.sendFile(`${p}/index.html`, {
- headers: {
- 'Content-Type': 'text/html'
- }
- });
- });
- app.post('/active', async (req, res) => {
- const { active } = req.body;
- try {
- await writeFile(configFile, JSON.stringify({ active }));
- } catch (error) {
- throw error;
- res.status(500).send(error);
- }
- res.status(200).send('Ok');
- });
- app.get('/active', async (req, res) => {
- let active;
- try {
- active = JSON.parse(await readFile(configFile));
- } catch (error) {
- throw error;
- res.status(500).send(error);
- }
- res.status(200).send(active);
- });
- app.listen(process.env.PORT, (err) => {
- if (err) throw err;
- console.log(`Server is running on localhost:${process.env.PORT}`);
- });
Add Comment
Please, Sign In to add comment