Skip to content

Commit

Permalink
Migrate to Express 4 and resolve deprecated notices
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentClair committed Sep 13, 2017
1 parent 311eb1a commit bd90f50
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 553 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
node_modules
.idea

config/development.yaml
config/production.yaml
config/runtime.json
.idea

node_modules
tmp
phantomjs
5 changes: 0 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ process.on('SIGINT', function () {
// web service
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(app.router);
app.set('rasterizerService', new RasterizerService(config.rasterizer).startService());
app.set('fileCleanerService', new FileCleanerService(config.cache.lifetime));

if (app.get('env') == 'development') {
app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
}

require('./routes')(app, config.server.useCors);
app.listen(config.server.port);
console.log('Express server listening on ' + config.server.host + ':' + config.server.port);
2 changes: 1 addition & 1 deletion config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ rasterizer:
command: phantomjs # phantomjs executable
port: 3001 # internal service port. No need to allow inbound or outbound access to this port
host: '127.0.0.1' # host to listen on
path: 'tmp/' # where the screenshot files are stored
path: /tmp # where the screenshot files are stored
viewport: '1024x600' # browser window size. Height frows according to the content
cache:
lifetime: 60000 # one minute, set to 0 for no cache
Expand Down
34 changes: 0 additions & 34 deletions example/client.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rasterizerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ RasterizerService.prototype.pingService = function () {
}
var self = this;
request('http://localhost:' + this.getPort() + '/healthCheck', function (error, response) {
if (error || response.statusCode != 200) {
if (error || response.statusCode !== 200) {
return;
}
self.lastHealthCheckDate = Date.now();
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"node": ">=0.8.4"
},
"dependencies": {
"config": "0.4.15",
"express": "3.x",
"request": "2.9.153"
"config": "^1.26.2",
"express": "^4.15.4",
"js-yaml": "^3.10.0",
"request": "^2.81.0"
},
"scripts": {
"start": "node app"
Expand Down
19 changes: 10 additions & 9 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var utils = require('../lib/utils');
var join = require('path').join;
var fs = require('fs');
var path = require('path');
var request = require('request');

module.exports = function (app, useCors) {
Expand All @@ -10,11 +9,12 @@ module.exports = function (app, useCors) {

// routes
app.get('/', function (req, res, next) {
if (!req.param('url', false)) {
var rawUrl = req.query.url || false;
if (!rawUrl) {
return res.redirect('/usage.html');
}

var url = utils.url(req.param('url'));
var url = utils.url(rawUrl);
// required options
var options = {
uri: 'http://localhost:' + rasterizerService.getPort() + '/',
Expand All @@ -32,8 +32,9 @@ module.exports = function (app, useCors) {
'password',
'delay'
].forEach(function (name) {
if (req.param(name, false)) {
options.headers[name] = req.param(name);
var header = req.query[name] || false;
if (header) {
options.headers[name] = header;
}
});

Expand All @@ -42,7 +43,7 @@ module.exports = function (app, useCors) {

var filePath = join(rasterizerService.getPath(), filename);

var callbackUrl = req.param('callback', false) ? utils.url(req.param('callback')) : false;
var callbackUrl = req.query.callback ? utils.url(req.query.callback) : false;

if (fs.existsSync(filePath)) {
console.log('Request for %s - Found in cache', url);
Expand Down Expand Up @@ -93,12 +94,12 @@ module.exports = function (app, useCors) {

var callRasterizer = function (rasterizerOptions, callback) {
request.get(rasterizerOptions, function (error, response, body) {
if (error || response.statusCode != 200) {
if (error || response.statusCode !== 200) {
console.log('Error while requesting the rasterizer: %s', error.message);
rasterizerService.restartService();
return callback(new Error(body));
}
else if (body.indexOf('Error: ') == 0) {
if (body.indexOf('Error: ') === 0) {
var errmsg = body.substring(7);
console.log('Error while requesting the rasterizer: %s', errmsg);
return callback(new Error(errmsg));
Expand Down Expand Up @@ -129,7 +130,7 @@ module.exports = function (app, useCors) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Expose-Headers", "Content-Type");
}
res.sendfile(imagePath, function (err) {
res.sendFile(imagePath, function (err) {
fileCleanerService.addFile(imagePath);
callback(err);
});
Expand Down
Loading

0 comments on commit bd90f50

Please sign in to comment.