Created
December 4, 2018 19:11
-
-
Save alexanmtz/0b0264042c7aa93e2d9608affa692e65 to your computer and use it in GitHub Desktop.
A server that will receive a route sensor with credentials to notify by e-mail the user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
const cors = require('cors') | |
const bodyParser = require('body-parser') | |
const Notify = require('./mail') | |
if (process.env.NODE_ENV !== 'production') { | |
require('dotenv').config() | |
app.use(cors()) | |
} | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({ extended: true })) | |
app.set('port', (process.env.PORT || 3000)) | |
app.get('/sensor', (req, res) => { | |
if(req.query.secret === process.env.SECRET) { | |
const response = req.query.humidity ? { humidity: req.query.humidity } : {} | |
if(response.humidity) { | |
Notify.sensor(response.humidity) | |
} | |
return res.json(response).end() | |
} | |
return res.status(500).end() | |
}) | |
app.listen(app.get('port'), () => { | |
// eslint-disable-next-line no-console | |
console.log('Node app is running on port', app.get('port')) | |
}) | |
module.exports = app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment