Add support for HEAD requests
On regular document endpoints, and on raw endpoints
This commit is contained in:
parent
219424550b
commit
c409aca080
|
@ -16,33 +16,55 @@ var DocumentHandler = function(options) {
|
||||||
DocumentHandler.defaultKeyLength = 10;
|
DocumentHandler.defaultKeyLength = 10;
|
||||||
|
|
||||||
// Handle retrieving a document
|
// Handle retrieving a document
|
||||||
DocumentHandler.prototype.handleGet = function(key, response, skipExpire) {
|
DocumentHandler.prototype.handleGet = function(request, response, config) {
|
||||||
|
const key = request.params.id.split('.')[0];
|
||||||
|
const skipExpire = !!config.documents[key];
|
||||||
|
|
||||||
this.store.get(key, function(ret) {
|
this.store.get(key, function(ret) {
|
||||||
if (ret) {
|
if (ret) {
|
||||||
winston.verbose('retrieved document', { key: key });
|
winston.verbose('retrieved document', { key: key });
|
||||||
response.writeHead(200, { 'content-type': 'application/json' });
|
response.writeHead(200, { 'content-type': 'application/json' });
|
||||||
response.end(JSON.stringify({ data: ret, key: key }));
|
if (request.method === 'HEAD') {
|
||||||
|
response.end();
|
||||||
|
} else {
|
||||||
|
response.end(JSON.stringify({ data: ret, key: key }));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
winston.warn('document not found', { key: key });
|
winston.warn('document not found', { key: key });
|
||||||
response.writeHead(404, { 'content-type': 'application/json' });
|
response.writeHead(404, { 'content-type': 'application/json' });
|
||||||
response.end(JSON.stringify({ message: 'Document not found.' }));
|
if (request.method === 'HEAD') {
|
||||||
|
response.end();
|
||||||
|
} else {
|
||||||
|
response.end(JSON.stringify({ message: 'Document not found.' }));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, skipExpire);
|
}, skipExpire);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle retrieving the raw version of a document
|
// Handle retrieving the raw version of a document
|
||||||
DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) {
|
DocumentHandler.prototype.handleRawGet = function(request, response, config) {
|
||||||
|
const key = request.params.id.split('.')[0];
|
||||||
|
const skipExpire = !!config.documents[key];
|
||||||
|
|
||||||
this.store.get(key, function(ret) {
|
this.store.get(key, function(ret) {
|
||||||
if (ret) {
|
if (ret) {
|
||||||
winston.verbose('retrieved raw document', { key: key });
|
winston.verbose('retrieved raw document', { key: key });
|
||||||
response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
|
response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
|
||||||
response.end(ret);
|
if (request.method === 'HEAD') {
|
||||||
|
response.end();
|
||||||
|
} else {
|
||||||
|
response.end(ret);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
winston.warn('raw document not found', { key: key });
|
winston.warn('raw document not found', { key: key });
|
||||||
response.writeHead(404, { 'content-type': 'application/json' });
|
response.writeHead(404, { 'content-type': 'application/json' });
|
||||||
response.end(JSON.stringify({ message: 'Document not found.' }));
|
if (request.method === 'HEAD') {
|
||||||
|
response.end();
|
||||||
|
} else {
|
||||||
|
response.end(JSON.stringify({ message: 'Document not found.' }));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, skipExpire);
|
}, skipExpire);
|
||||||
};
|
};
|
||||||
|
|
20
server.js
20
server.js
|
@ -109,20 +109,28 @@ if (config.rateLimits) {
|
||||||
// first look at API calls
|
// first look at API calls
|
||||||
app.use(route(function(router) {
|
app.use(route(function(router) {
|
||||||
// get raw documents - support getting with extension
|
// get raw documents - support getting with extension
|
||||||
|
|
||||||
router.get('/raw/:id', function(request, response) {
|
router.get('/raw/:id', function(request, response) {
|
||||||
var key = request.params.id.split('.')[0];
|
return documentHandler.handleRawGet(request, response, config);
|
||||||
var skipExpire = !!config.documents[key];
|
|
||||||
return documentHandler.handleRawGet(key, response, skipExpire);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.head('/raw/:id', function(request, response) {
|
||||||
|
return documentHandler.handleRawGet(request, response, config);
|
||||||
|
});
|
||||||
|
|
||||||
// add documents
|
// add documents
|
||||||
|
|
||||||
router.post('/documents', function(request, response) {
|
router.post('/documents', function(request, response) {
|
||||||
return documentHandler.handlePost(request, response);
|
return documentHandler.handlePost(request, response);
|
||||||
});
|
});
|
||||||
|
|
||||||
// get documents
|
// get documents
|
||||||
router.get('/documents/:id', function(request, response) {
|
router.get('/documents/:id', function(request, response) {
|
||||||
var key = request.params.id.split('.')[0];
|
return documentHandler.handleGet(request, response, config);
|
||||||
var skipExpire = !!config.documents[key];
|
});
|
||||||
return documentHandler.handleGet(key, response, skipExpire);
|
|
||||||
|
router.head('/documents/:id', function(request, response) {
|
||||||
|
return documentHandler.handleGet(request, response, config);
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue