improved express setup (added helmet, morgan)
This commit is contained in:
parent
fc3a4b31c1
commit
ba9dd7d17a
3 changed files with 127 additions and 18 deletions
|
|
@ -1,29 +1,29 @@
|
|||
import express from 'express';
|
||||
import bodyParser from "body-parser";
|
||||
|
||||
import pgPromise from "pg-promise";
|
||||
const pgp = pgPromise({});
|
||||
const db = pgp('postgres://postgres:postgres@localhost:5432/rr')
|
||||
import helmet from "helmet";
|
||||
import bodyParser from "body-parser";
|
||||
import morgan from 'morgan';
|
||||
|
||||
const app = express()
|
||||
const port = 3000
|
||||
|
||||
app.use(bodyParser.json())
|
||||
app.use(helmet())
|
||||
|
||||
app.get('/test', (req, res) => {
|
||||
res.send(JSON.stringify({success: true}))
|
||||
})
|
||||
// init database connection
|
||||
const pgp = pgPromise({});
|
||||
const db = pgp('postgres://postgres:postgres@localhost:5432/rr')
|
||||
|
||||
app.post('/highscore', async (req, res) => {
|
||||
if (req.body !== undefined) {
|
||||
let data = await db.any(
|
||||
'SELECT * FROM lb_highscore LIMIT $1 OFFSET $2',
|
||||
[req.body.itemsPerPage, req.body.itemsPerPage * (req.body.page - 1)]
|
||||
)
|
||||
res.send(data)
|
||||
} else {
|
||||
res.status(400)
|
||||
res.send("itemsPerPage and/or page not defined")
|
||||
}
|
||||
// configure & use logger
|
||||
let morganFormatted = morgan('[:date[iso]] :method :url - :status')
|
||||
app.use(morganFormatted);
|
||||
|
||||
|
||||
app.get('/highscore', async (req, res) => {
|
||||
let data = await db.any(
|
||||
'SELECT * FROM lb_highscore;'
|
||||
)
|
||||
res.send(data)
|
||||
})
|
||||
|
||||
app.listen(port, () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue