Created
January 8, 2022 21:09
-
-
Save okdistribute/8577a591087f1818097da868c84c992c to your computer and use it in GitHub Desktop.
Automerge Tutorial Server
This file contains 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
{ | |
"name": "server", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.19.0", | |
"cors": "^2.8.5", | |
"express": "^4.17.1", | |
"pump": "^3.0.0" | |
} | |
} |
This file contains 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
const express = require('express') | |
const path = require('path') | |
const fs = require('fs') | |
const cors = require('cors') | |
const bodyParser = require('body-parser'); | |
let app = express() | |
var options = { | |
inflate: true, | |
limit: '100kb', | |
type: 'application/octet-stream' | |
}; | |
app.use(bodyParser.raw(options)); | |
try { | |
fs.mkdirSync(path.join(__dirname, 'data')) | |
} catch (err) { | |
if (err.code !== 'EEXIST') { | |
console.error(err) | |
} | |
} | |
app.use(cors()) | |
app.get('/:id', (req, res) => { | |
let id = req.params.id | |
let filename = path.join(__dirname, 'data', id) | |
fs.stat(filename, (err, stats) => { | |
if (err) { | |
console.error(err) | |
res.status(404).send('Not found') | |
} else { | |
res.sendFile(filename) | |
console.log('sending') | |
} | |
}) | |
}) | |
app.post('/:id', (req, res) => { | |
let id = req.params.id | |
fs.writeFileSync(path.join(__dirname, 'data', id), req.body) | |
res.status(200).send('ok') | |
}) | |
const port = 5000 | |
app.listen(5000, () => { | |
console.log('listening on http://localhost:' + port) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment