-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
32 lines (26 loc) · 968 Bytes
/
app.js
File metadata and controls
32 lines (26 loc) · 968 Bytes
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
const express = require('express')
const fetch = require('node-fetch')
const app = express()
const path = require('path')
const PORT = process.env.PORT || 8000; // process.env accesses heroku's environment variables
require("dotenv").config();
app.get("/api/search",(req, res) => {
fetch(`https://customsearch.googleapis.com/customsearch/v1?key=${process.env.API_KEY}&cx=${process.env.CX}&q=${req.query.q}`,{
method: 'GET',
mode: 'cors'
} )
.then(response => {
if(!response.ok) throw('you did a bad')
return response.json()
}).then(data => {
res.status(200).json( data )
})
})
app.use(express.static('public'))
app.get('/', (request, res) => {
res.sendFile(path.join(__dirname, './public/index.html'))
})
app.listen(PORT, () => {
console.log(__dirname);
console.log(`listening on ${PORT}`)
})