This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
SA3/hw7/Claudio_Maggioni/routes/fetch_tests/router.js

73 lines
1.5 KiB
JavaScript

'use strict';
const express = require('express');
const router = express.Router();
//These routes should be activated under /test/fetch so that they can be called by the test.html
//when testing your fetch wrappers doFetchRequest, doJSONRequest found in the public/js/fetch.js
//Add the following to your app.js:
//app.use('/test/fetch', routers.fetch_tests);
router.get('/', function(req, res, next) {
if(req.accepts('json')) {
res.json({
text: 'GET Working'
})
} else {
res.status(200).end('GET Working')
}
});
router.all('/json/ok', function(req, res, next) {
res.json({
text: 'ok'
});
});
router.delete('/', function(req, res, next) {
if(req.accepts('json')) {
res.json({
status: 204,
text: 'DELETE Working'
})
} else {
res.status(204).end()
}
});
router.get('/redirect', function(req, res, next) {
res.redirect('redirected')
});
router.get('/redirected', function(req, res, next) {
res.status(200).end('Redirect Works')
});
router.post('/new', function(req, res, next) {
if(req.accepts('json')) {
res.type('json');
res.set('Location', '24');
res.status(201).json({
text: 'POST Working',
body: req.body
})
} else {
res.set('Location', '42');
res.status(201).end('POST Working\n'+req.body)
}
});
router.put('/echo', function(req, res, next) {
if(req.accepts('json')) {
res.json({
text: 'PUT Working',
body: req.body
})
} else {
res.end(req.body)
}
});
module.exports = router;