Created
April 6, 2019 05:16
-
-
Save Qrbaker/65678a18f133f6ba365d09abc70b79b3 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
var express = require('express'); | |
var chalk = require('chalk'); | |
var app = express(); | |
var router = express.Router(); | |
var port = process.env.PORT || 8080; | |
router.get('/test', function(req, res) { | |
res.status(200).send('Hello world'); | |
}); | |
app.use('/api', router); | |
app.get('/rest/list/', function(req,res) { | |
res.json(tickets) | |
}); | |
router.get('/rest/ticket/id', function(req, res) { | |
var ticket = tickets.find( x=> x.id === req.params.id) | |
if(ticket != null) { | |
res.json(ticket) | |
} else { | |
res.sendStatus(404) | |
} | |
}); | |
app.post('/rest/ticket/', function(req,res) { | |
//validation | |
tickets.push(req.params) | |
}); | |
// magic of validation of objects and error handling | |
app.listen(port, function(err) { | |
if (err) { | |
console.log(chalk.red(err)); | |
} else { | |
console.log(chalk.blue('Magic Happens on Port 69')); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment