Created
August 15, 2019 15:47
-
-
Save prof3ssorSt3v3/c0e7510543e7868271bd5c2a0f58be2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// A BASIC Node server listening for environment variables | |
const http = require("http"); | |
const url = require("url"); | |
const debug = require("debug")("app"); | |
//listen for env variable NODE_ENV = development | production | |
const env = process.env.NODE_ENV ? process.env.NODE_ENV : "development"; | |
const port = env === "development" ? 3000 : 5000; | |
const server = http.createServer(function(req, res) { | |
debug(req.url, req.method); | |
debug(req.headers); | |
res.setHeader("Content-type", "application/json"); | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.writeHead(200, "OK", { "Content-Type": "application/json" }); | |
let dataObj = { id: 123, name: "Bob", email: "[email protected]" }; | |
let data = JSON.stringify(dataObj); | |
res.end(data); | |
}); | |
server.listen(port, function() { | |
console.log(`Listening for ${env} on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment