Created
May 14, 2017 23:03
-
-
Save jbaldwinroberts/546e7cb1289856681c4d53f1cd9227b9 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
| // BASE SETUP | |
| // ============================================================================= | |
| // call the packages we need | |
| var express = require('express'); // call express | |
| var app = express(); // define our app using express | |
| var bodyParser = require('body-parser'); | |
| // configure app to use bodyParser() | |
| // this will let us get the data from a POST | |
| app.use(bodyParser.urlencoded({ extended: true })); | |
| app.use(bodyParser.json()); | |
| var port = process.env.PORT || 8080; // set our port | |
| // ROUTES FOR OUR API | |
| // ============================================================================= | |
| var router = express.Router(); // get an instance of the express Router | |
| // test route to make sure everything is working (accessed at GET http://localhost:8080/api) | |
| router.get('/', (req, res) => { | |
| res.json({ message: 'hooray! welcome to our api!' }); | |
| }); | |
| // more routes for our API will happen here | |
| router.route('/controller') | |
| .get((req, res) => { | |
| id = register(); | |
| console.log('Get CONTROLLER: ', id); | |
| res.json(id); | |
| }); | |
| router.route('/controller/:controller_id/event') | |
| .post((req, res) => { | |
| console.log('EVENT'); | |
| // console.log(req.params.controller_id); | |
| console.log(req.body); | |
| if (req.body.state == 'press') { | |
| snake.move(req.body.value); | |
| } | |
| res.status(200).json("yay"); | |
| }); | |
| // REGISTER OUR ROUTES ------------------------------- | |
| // all of our routes will be prefixed with /api | |
| app.use('/api', router); | |
| // START THE SERVER | |
| // ============================================================================= | |
| app.listen(port); | |
| console.log('Magic happens on port ' + port); | |
| var i = 0; | |
| const register = () => { | |
| return i++; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment