-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (52 loc) · 2.06 KB
/
server.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
64
65
'use strict';
const express = require('express');
const path = require('path');
const serverless = require('serverless-http');
const app = express();
const bodyParser = require('body-parser');
const axios = require('axios').default;
const cors = require('cors');
const mongoose = require('mongoose');
const Todo = require('../modal/todo.model');
const router = express.Router();
app.use(cors());
app.use(bodyParser.json());
require('dotenv').config();
mongoose.connect(`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTER}/test?retryWrites=true&w=majority`, { useNewUrlParser: true, useUnifiedTopology: true });
const connection = mongoose.connection;
connection.once('open', function () {
console.log("MongoDB database connection established successfully");
})
router.get('/', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<h1>Hello from Express.js!</h1>');
res.end();
});
router.get('/todos', (req, res) => {
Todo.find(function (err, todos) {
if (err) {
console.log(err);
} else {
res.json(todos);
}
});
});
router.post('/vac', async (req, res) => {
console.log("###########################");
console.log(req.headers);
console.log("#################");
//const request = await axios.get('https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=651&date=23-05-2021', {
const request = await axios.get('https://www.npmjs.com/package/axios', {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}
})
res.json(request.data);
});
router.get('/another', (req, res) => res.json({ route: req.originalUrl }));
router.post('/', (req, res) => res.json({ postBody: req.body }));
app.use(bodyParser.json());
app.use('/.netlify/functions/server', router); // path must route to lambda
app.use('/', (req, res) => res.sendFile(path.join(__dirname, '../index.html')));
module.exports = app;
module.exports.handler = serverless(app);