Created
June 19, 2018 16:05
-
-
Save Dviejopomata/480d2d2b5edfb1cd651c53210aea4bda to your computer and use it in GitHub Desktop.
Prueba arduino web api
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
{ | |
"name": "node-server", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"@types/express": "^4.16.0", | |
"@types/node": "^10.3.4", | |
"body-parser": "^1.18.3", | |
"express": "^4.16.3", | |
"lodash": "^4.17.10" | |
} | |
} |
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 lodash = require("lodash") | |
const app = express() | |
const bodyparser = require("body-parser") | |
app.use(bodyparser.json()) | |
app.use(bodyparser.urlencoded({ extended: true })) | |
const distancias = [] | |
app.get("/", (req, res) => { | |
res.send( | |
lodash.sortBy(distancias, function(distancia) { | |
return -distancia.fecha.getTime() | |
}), | |
) | |
}) | |
app.post("/", (req, res) => { | |
const apiKey = req.headers["X-APIKEY"] | |
if (apiKey !== "MI_API_KEY") { | |
return res.sendStatus(403) | |
} | |
console.log(req.body) | |
distancias.push({ | |
distancia: req.body.Distancia, | |
fecha: new Date(), | |
}) | |
res.send(`Your body is ${JSON.stringify(req.body)}`) | |
}) | |
app.get("/search", (req, res) => { | |
const { nombre = "FRAN MIRA" } = req.params | |
res.send(`Hola ${nombre}`) | |
}) | |
app.listen(9005) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment