Skip to content

Instantly share code, notes, and snippets.

@Qrbaker
Created April 6, 2019 05:16
Show Gist options
  • Save Qrbaker/65678a18f133f6ba365d09abc70b79b3 to your computer and use it in GitHub Desktop.
Save Qrbaker/65678a18f133f6ba365d09abc70b79b3 to your computer and use it in GitHub Desktop.
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