dgf-prototype/backend/src/index.js

35 lines
847 B
JavaScript
Raw Normal View History

2024-03-19 22:22:36 -05:00
const express = require('express');
2024-04-20 12:37:59 -05:00
const read = require('./read');
const write = require('./write');
const importFromSS = require('./import-from-ss');
2024-03-19 22:22:36 -05:00
require('dotenv').config();
2024-04-21 12:51:58 -05:00
require('express-async-errors');
2024-03-19 22:22:36 -05:00
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.post('/write', write);
app.get('/read/:hash', read);
app.post('/importFromSemanticScholar', importFromSS);
2024-03-19 22:22:36 -05:00
app.get('*', (req, res) => {
2024-04-04 12:55:59 -05:00
console.log(`404 req.path: ${req.path}`);
res.status(404).json({ errorCode: 404 });
2024-03-19 22:22:36 -05:00
});
2024-04-21 12:51:58 -05:00
app.use((err, req, res, next) => {
const status = err.response?.status ?? 500;
const message = err.response?.data?.error ?? err.message;
2024-04-22 13:52:04 -05:00
console.error(`error: ${message}`, err);
2024-04-21 12:51:58 -05:00
res.status(status).send(message);
next();
});
2024-03-19 22:22:36 -05:00
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});