-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (51 loc) · 1.49 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const dotenv = require("dotenv");
const expressValidator = require("express-validator");
const cookieParser = require("cookie-parser");
const fs = require('fs');
const cors = require("cors");
dotenv.config();
mongoose.connect(
process.env.MONGO_URI,
{useNewUrlParser:true, useUnifiedTopology: true }
).then(() => console.log("DB connected"));
mongoose.connection.on("error", err => {
console.log(`DB connection error:${err.message}`);
});
// bring in routes
const postRoutes = require('./routes/post');
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/user');
// middleware
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(expressValidator());
app.get('/api', (req, res) => {
fs.readFile('docs/apiDocs.json', (err, data) => {
if (err) {
res.status(400).json({
error: err
});
}
const docs = JSON.parse(data);
res.json(docs);
});
});
app.use(cors());
app.use("/",postRoutes);
app.use("/",authRoutes);
app.use("/",userRoutes);
app.use(function(err,req,res,next){
if(err.name ==='UnauthorizedError'){
res.status(401).json({error:"Unauthorized"});
}
})
const port = 8080;
app.listen(port,() => {
console.log(`A Node JS API is listening on port ${port}`);
})