-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
42 lines (34 loc) · 1.38 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
// Libraries
const express = require ('express'); // web app framework
const fetch = require('node-fetch'); // library for making requests
const dotenv = require('dotenv').config() // makes it easy to work with secret keys stored in .env
// any available / unused port number will do fine
const port = 7000;
// Express Setup
const app = express();
app.use( express.json() );
// Enables node to serve up files inside of /public/ folder
// this includes static files (index.html, images, CSS, etc.)
app.use('/PlantFinder', express.static('public') );
app.get('/PlantFinder/token/', (req, res) => {
// https://docs.trefle.io/docs/advanced/client-side-apps
// NOTE: there are a couple of ways to get the correct host depending on the environment.
// i.e. if 'x-forwarded-host' doesn't work for you, try 'host' instead:
// let ip = req.headers['host'];
let ip = req.headers['x-forwarded-host'];
fetch( 'https://trefle.io/api/auth/claim', {
method: 'post',
body: JSON.stringify({
origin: 'https://'+ip,
token: process.env.TREFLE_TOKEN
}),
headers: { 'Content-Type': 'application/json' }
})
.then(response => response.json())
.then(result => res.send( result ) )
.catch(error => console.log('error', error));
});
//Go live
app.listen(port, () => {
console.log("We are live on " + port);
});