Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata
Created June 19, 2018 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dviejopomata/480d2d2b5edfb1cd651c53210aea4bda to your computer and use it in GitHub Desktop.
Save Dviejopomata/480d2d2b5edfb1cd651c53210aea4bda to your computer and use it in GitHub Desktop.
Prueba arduino web api
{
"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"
}
}
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