Last active
March 14, 2018 07:50
-
-
Save ttristan/f11dbb690db902f056ea33482bb10891 to your computer and use it in GitHub Desktop.
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 bodyParser = require("body-parser"); | |
const path = require("path"); | |
const cors = require("cors"); | |
// Initialize http server | |
const app = express(); | |
// setup | |
app.use(express.static(path.resolve(__dirname, "../build"))); | |
app.use(cors()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json({ limit: "10mb" })); | |
//routes | |
app.get("/", (request, response) => { | |
console.log(Date.now(), "get /"); | |
response.json({ status: "200" }); | |
response.end("success"); | |
}); | |
app.get("/x", (request, response) => { | |
const { query = {} } = request; | |
console.log(Date.now(), "query", query); | |
Promise.resolve({ params: query }) | |
.then(() => { | |
response.json({ query }); | |
response.end("success"); | |
}) | |
.catch(error => { | |
console.log("error", error); | |
response.json(error); | |
response.end("error"); | |
}); | |
}); | |
app.post("/y", (request, response) => { | |
const { body = {} } = request; | |
const { params } = body; | |
console.log(Date.now(), "body", body); | |
Promise.resolve({ params: body }) | |
.then(() => { | |
response.json({ body }); | |
response.end("success"); | |
}) | |
.catch(error => { | |
console.log("error", error); | |
response.json(error); | |
response.end("error"); | |
}); | |
}); | |
// launch server | |
const server = app.listen(1024, () => { | |
const { address, port } = server.address(); | |
console.log(Date.now(), `Listening at http://${address}:${port}`); | |
}); | |
process.on("uncaughtException", err => { | |
if (err.code === "ENOTFOUND") return; | |
if (err.code === "ECONNRESET") return; | |
if (err.code === "PROTOCOL_CONNECTION_LOST") return; | |
console.log(Date.now(), "UNCAUGHT ERROR", err); | |
console.log(Date.now(), "UNCAUGHT ERROR", err.code); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment