Created
September 19, 2015 17:15
-
-
Save robey/a6f4a2cbd177176fa24e to your computer and use it in GitHub Desktop.
simple restify API server for answering XHR requests
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
"use strict"; | |
import restify from "restify"; | |
const PORT = 8080; | |
export function main() { | |
const server = restify.createServer({ name: "api" }); | |
server.use(restify.queryParser()); | |
server.use((request, response, next) => { | |
response.header("Access-Control-Allow-Origin", "*"); | |
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
server.get("/ping/:word", (request, response, next) => { | |
const word = request.params.word; | |
try { | |
response.send({ pong: word }); | |
} catch (error) { | |
response.send(error); | |
} | |
return next(); | |
}); | |
server.listen(PORT, () => { | |
process.stderr.write("api listening on port " + PORT + "\n"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment