Skip to content

Instantly share code, notes, and snippets.

@fisherds
Last active February 16, 2022 02:36
Show Gist options
  • Select an option

  • Save fisherds/9c4d7b861bfde8e6df5ddb53ca0826af to your computer and use it in GitHub Desktop.

Select an option

Save fisherds/9c4d7b861bfde8e6df5ddb53ca0826af to your computer and use it in GitHub Desktop.
Starting point for boilerplate app.js code.
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