-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (53 loc) · 1.54 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
50
51
52
53
54
55
56
57
58
59
const express = require("express");
const app=express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const cors = require("cors");
const morgan = require("morgan")
const axios = require('axios');
const helmet = require("helmet");
const Userrouter =require("./routes/user")
const post =require("./routes/post")
const cookieParser = require("cookie-parser");
dotenv.config();
app.use(cors({
origin: true,
credentials: true
}));
app.options('*', cors())
const isSecure = process.env.NODE_ENV === 'production';
app.get('/images', async (req, res) => {
try {
const image = req.query.image;
const response = await axios.get(`https://pixabay.com/api/?key=38407053-d222e14d96ceb4a030fdbc329&q=${image}&image_type=photo`);
res.send(response.data);
} catch (error) {
res.status(500).send({ error: error.toString() });
}
});
const database = (module.exports = () => {
const connectionParams = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
try {
mongoose.connect(
"mongodb+srv://nik:[email protected]/social-app?retryWrites=true&w=majority",
connectionParams
);
console.log("Database connected succesfully");
} catch (error) {
console.log(error);
console.log("Database connection failed");
}
});
database();
app.use(express.json());
app.use(cookieParser());
app.use(helmet());
app.use(morgan("common"));
app.use('/api', Userrouter);
app.use('/api', post);
app.listen(8800,()=>{
console.log("ready");
})