-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (36 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const lowDb = require('lowdb');
const swaggerUI = require('swagger-ui-express');
const swaggerJsDoc = require('swagger-jsdoc');
const booksRouter = require('./routes/books');
const PORT = process.env.PORT || 4000;
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = lowDb(adapter);
db.defaults({ books: [] }).write();
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Library API',
version: '1.0.0',
description: 'A simple Express Library API',
},
servers: [{ url: 'http://localhost:4000' }],
},
apis: ['./routes/*.js'],
};
const specs = swaggerJsDoc(options);
const app = express();
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(specs));
app.db = db;
app.use(cors());
app.use(express.json());
app.use(morgan('dev'));
app.get('/', (_req, res) => {
res.json('Welcome to the Library API');
});
app.use('/books', booksRouter);
app.listen(PORT, () => console.log(`The server is running on port ${PORT}`));