Skip to content

Instantly share code, notes, and snippets.

@okdistribute
Created January 8, 2022 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okdistribute/8577a591087f1818097da868c84c992c to your computer and use it in GitHub Desktop.
Save okdistribute/8577a591087f1818097da868c84c992c to your computer and use it in GitHub Desktop.
Automerge Tutorial Server
{
"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"
}
}
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