Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 4dc517f

Browse files
committed
Added 'hackyourinfo' app - a small web server for students to POST info to as a learning aid in Week 1.
Usage: - Deploy somehwere (e.g. using now: `now -p -n hackyourinfo`) - Give URL to users - Endpoints: POST /:name.json : Post any JSON body to save info for a student. GET /:name.json : Retrieve that student JSON body GET /_all.json : Retrieve all students' info DELETE /:name.json : Delete one student's info. DELETE /_all.json : Start over
1 parent e4b19cb commit 4dc517f

3 files changed

Lines changed: 502 additions & 0 deletions

File tree

apps/hackyourinfo/index.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const express = require('express')
2+
const fs = require('fs-extra')
3+
const path = require('path')
4+
const bodyParser = require('body-parser')
5+
const cors = require('cors')
6+
7+
const rootDir = path.resolve(__dirname)
8+
const infoDir = path.join(rootDir, 'info')
9+
10+
const app = express()
11+
12+
app.use(bodyParser.json())
13+
app.use(cors())
14+
15+
app.post('/:name.json', async (request, response) => {
16+
try {
17+
const filename = path.join(infoDir, `${request.params.name}.json`)
18+
await fs.writeFile(filename, JSON.stringify(request.body, null, 2))
19+
response.statusCode = 200
20+
response.json({ok: true})
21+
} catch (error) {
22+
response.statusCode = 500
23+
response.json({error: "An error occurred"})
24+
console.error(error)
25+
}
26+
27+
response.end()
28+
})
29+
30+
app.get('/_all.json', async (request, response) => {
31+
try {
32+
const files = await fs.readdir(infoDir)
33+
const promises = files.map(file => fs.readFile(path.join(infoDir, file), 'utf8').then(raw => [file, raw]))
34+
const raws = await Promise.all(promises)
35+
36+
const result = {}
37+
for (const [file, raw] of raws) {
38+
result[file.replace(/\.json$/, '')] = JSON.parse(raw)
39+
}
40+
response.json(result)
41+
} catch (error) {
42+
response.statusCode = 500
43+
response.json({error: "An error occurred"})
44+
console.error(error)
45+
}
46+
response.end()
47+
})
48+
49+
app.get('/:name.json', async (request, response) => {
50+
try {
51+
const filename = path.join(infoDir, `${request.params.name}.json`)
52+
const raw = await fs.readFile(filename, 'utf8')
53+
response.json(JSON.parse(raw))
54+
} catch (error) {
55+
if (error.code === 'ENOENT') {
56+
response.statusCode = 404
57+
response.json({error: "File not found"})
58+
} else {
59+
response.statusCode = 500
60+
response.json({error: "An error occurred"})
61+
console.error(error)
62+
}
63+
}
64+
response.end()
65+
})
66+
67+
app.delete('/_all.json', async (request, response) => {
68+
try {
69+
await fs.emptyDir(infoDir)
70+
response.json({ok: true})
71+
} catch (error) {
72+
response.statusCode = 500
73+
response.json({error: "An error occurred"})
74+
console.error(error)
75+
}
76+
response.end()
77+
})
78+
79+
app.delete('/:name.json', async (request, response) => {
80+
try {
81+
await fs.unlink(path.join(infoDir, `${request.params.name}.json`))
82+
response.json({ok: true})
83+
} catch (error) {
84+
if (error.code !== 'ENOENT') {
85+
response.statusCode = 500
86+
response.json({error: "An error occurred"})
87+
console.error(error)
88+
} else {
89+
response.json({ok: true})
90+
}
91+
}
92+
response.end()
93+
})
94+
95+
app.use((request, response) => {
96+
response.statusCode = 404
97+
response.json({error: "Not found"})
98+
response.end()
99+
})
100+
101+
app.listen(process.env.PORT || 80)

apps/hackyourinfo/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "hackyourinfo",
3+
"version": "1.0.0",
4+
"description": "Small webserver for class collaboration.",
5+
"main": "index.js",
6+
"author": "Joost Lubach",
7+
"license": "MIT",
8+
"private": false,
9+
"dependencies": {
10+
"body-parser": "^1.18.3",
11+
"cors": "^2.8.5",
12+
"express": "^4.16.4",
13+
"fs-extra": "^7.0.1"
14+
},
15+
"scripts": {
16+
"start": "node ."
17+
}
18+
}

0 commit comments

Comments
 (0)