You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB
JavaScript

import express from 'express'
// import serveStatic from 'serve-static'
import bodyParser from 'body-parser'
import { loggingMiddleware, PingRouter, StatusRouter } from './server/helpers.js'
import { createDatabase, getUser, updateUser } from './db/database.js'
const app = express()
const db = createDatabase('./db.local.json', {
users: {
BachoSeven: {
email: 'foo@bar.com',
password: '123',
},
},
problems: {},
})
app.use(bodyParser.json())
app.use(loggingMiddleware)
app.use('/api/status', new StatusRouter())
app.use('/api/ping', new PingRouter())
app.get('/api/user/:id', async (req, res) => {
const user = await getUser(db, req.params.id)
if (user) {
res.json(user)
} else {
res.sendStatus(404)
}
})
app.post('/api/user/:id', async (req, res) => {
await updateUser(db, req.params.id, req.body)
res.sendStatus(200)
})
app.all('*', (_req, res) => {
res.sendStatus(404)
})
app.listen(4000, () => {
console.log(`Started server on port 4000...`)
})