/** * Ensure JSON acceptance */ app.use((req, res, next) => { let err if (!req.accepts('json')) { err = new Error('Not Acceptable') err.status = 406 } return next(err) })
app.post('/process', function(req, res){ if(req.xhr || req.accepts('json,html')==='json'){ // if there were an error, we would send { error: 'error description' } res.send({ success: true }); } else { // if there were an error, we would redirect to an error page res.redirect(303, '/thank-you'); } });
router.get('/', (req, res) => { const metrics = typeof collection.name === 'object' ? collection.toJSON()[collection.name] : collection.toJSON(); if (req.accepts('html')) { res.render(path.join(__dirname, 'index.html'), { metrics: JSON.stringify(metrics, 0, 2) }); } else { res.json(metrics); } });
app.get('/', function (req, res) { res.status(201); if (req.accepts('text')) { res.write('name; email\n') res.write('William Bruno; wbruno@gmail.com\n'); res.end(); } else { res.json({ 'name': 'William Bruno', 'email': 'wbrunom@gmail.com' }); } });
// Update item router.put('/items/:id', function(req, res, next) { req.accepts('application/json'); var _id = req.params.id; Item.findById(_id, function(err, data) { if (err) { res.status(404).send(); } else { data.name = req.body.name; data.category = req.body.category; data.count = req.body.count; data.save(); res.status(200).json(data); } }); });
router.get('/', (req, res) => { exports.getHealthCheckResult() .then(result => { res.status(isHealthcheckCompletelyHealthy(result) ? 200 : 500); if (req.accepts('html')) { res.render(path.join(__dirname, 'index.html'), { result: JSON.stringify(result, 0, 2) }); } else { res.json(result); } }) .catch(err => { logger.error('Failed to run healthchecks', err); res.sendStatus(500); }); });
// Add new item router.post('/items', function(req, res, next) { req.accepts('application/json'); var item = { name: req.body.name, category: req.body.category, count: req.body.count }; var data = new Item(item); data.save(function(err) { if (err) { res.status(500).send(); } else { res.status(201).send(data._id); } }); });
router.get('/', (req, res) => { if (req.accepts('html')) { res.render(path.join(__dirname, 'index.html'), { config: JSON.stringify(config, 0, 2) }); } else { res.json(config); } });
if (we.plugins['we-plugin-url-alias'] && req.haveAlias && req.accepts('html')) {
router.get('/', (req, res) => { if (req.accepts('html')) { res.render(path.join(__dirname, 'index.html'), { report: nodeReport.getReport() }); } else { res.send(nodeReport.getReport()); } });
router.get('/', (req, res) => { if (req.accepts('html')) { res.render(path.join(__dirname, 'index.html'), { env: JSON.stringify(process.env, 0, 2) }); } else { res.json(process.env); } });
/* app.post('/process', function(req, res){ console.log('Form (from querystring): ' + req.query.form); console.log('CSRF token (from hidden form field): ' + req.body._csrf); console.log('Name (from visible form field): ' + req.body.name); console.log('Email (from visible form field): ' + req.body.email); res.redirect(303, '/thank-you'); }); */ //Handle form useing AJAX app.post('/ajaxprocess', function(req, res){ if(req.xhr || req.accepts('json,html')==='json'){ // if there were an error, we would send { error: 'error description' } res.send({ success: true }); } else { // if there were an error, we would redirect to an error page res.redirect(303, '/thank-you'); } });