Create a middleware to add a server timestamp header in milliseconds. Use for Express
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install --save server-timestamp
var serverTimestamp = require('server-timestamp');
Create a middleware that adds a X-Server-Timestamp
header to responses. If
you don't want to use this module to automatically set a header, please
see the section about Options format
The serverTimestamp
function accepts an optional options
object that may
contain any of the following keys:
The name of the header to set, defaults to X-Server-Timestamp.
This is a function that formats timestamps.
var serverTimestamp = require('./');
var express = require('express');
var app = express();
app.use(serverTimestamp());
// response
app.get('/', function (req, res) {
/*
res results:
{
"x-powered-by": "Express",
"x-server-timestamp": 1493365865576
}
*/
res.send(res._headers)
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
var serverTimestamp = require('./');
var express = require('express');
var app = express();
app.use(serverTimestamp({header: 'Example-Server-Timestamp'}));
// response
app.get('/', function (req, res) {
/*
res results:
{
"x-powered-by": "Express",
"example-server-timestamp": 1493365865576
}
*/
res.send(res._headers)
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
var serverTimestamp = require('./');
var express = require('express');
var app = express();
app.use(serverTimestamp({
header: 'Example-Format-Server-Timestamp',
format: function(timestamp){
var now = new Date(timestamp);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
return year + '-' + month + '-' + date + ' '+ hour + ':' + minute + ':' + second;
}
}));
// response
app.get('/', function (req, res) {
/*
res results:
{
"x-powered-by": "Express",
"example-format-server-timestamp": "2017-4-28 15:51:5"
}
*/
res.send(res._headers)
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
Check this repo for full example with Express
.