Last active
February 16, 2022 02:36
-
-
Save fisherds/9c4d7b861bfde8e6df5ddb53ca0826af to your computer and use it in GitHub Desktop.
Starting point for boilerplate app.js code.
This file contains hidden or 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
| var express = require("express"); | |
| var app = express(); | |
| app.use('/', express.static("public")); | |
| app.use('/api/', express.json()); | |
| let data = {}; | |
| // let data = []; // or an empty array depending on the instructions for your specific exam | |
| const fs = require("fs"); | |
| const serverSideStorage = "../data/db.json"; | |
| fs.readFile(serverSideStorage, function (err, buf) { | |
| if (err) { | |
| console.log("error: ", err); | |
| } else { | |
| data = JSON.parse(buf.toString()); | |
| } | |
| console.log("Data read from file."); | |
| }); | |
| function saveToServer(data) { | |
| fs.writeFile(serverSideStorage, JSON.stringify(data), function (err, buf) { | |
| if (err) { | |
| console.log("error: ", err); | |
| } else { | |
| console.log("Data saved successfully!"); | |
| } | |
| }) | |
| } | |
| // TODO: Create your backend API here: | |
| app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment