Browse Source

Started Express server

v1.x
Tj Holowaychuk 14 years ago
parent
commit
88912c4c50
  1. 4
      Readme.md
  2. 68
      test/server.js
  3. 6
      test/views/layout.jade
  4. 1
      test/views/tests.jade

4
Readme.md

@ -83,10 +83,6 @@ Visual tests:
$ node test/server.js
or provide a port:
$ sudo node test/server.js 80
## License
(The MIT License)

68
test/server.js

@ -3,46 +3,28 @@
* Module dependencies.
*/
var http = require('http')
, fs = require('fs');
var args = process.argv.slice(2)
, port = args.length
? parseInt(args[0], 10)
: 3000;
var images = fs.readdirSync(__dirname + '/images').sort();
function list(images) {
return '<table><tr><td></td><td>Test</td><td>Reference</td></tr>' + images.map(function(path, i){
if ('.gitignore' == path) return '';
return '<tr>'
+ '<td>' + i + '</td>'
+ '<td><img src="/images/' + path + '" style="border: 1px solid #eee; margin-right: 5px"/></td>'
+ '<td><img src="/references/' + path + '" style="border: 1px solid #eee"/></td>'
+ '</tr>';
}).join('') + '</table>';
}
http.createServer(function(req, res){
switch (req.url) {
case '/':
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(list(images));
break;
default:
fs.readFile(__dirname + '/' + req.url, function(err, buf){
if (err || !buf) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<p>Not Found</p>');
} else {
res.writeHead(200, {
'Content-Type': 'image/png'
, 'Content-Length': buf.length
});
res.end(buf);
}
});
}
}).listen(port);
console.log('Test image server started on port ' + port);
var express = require('../support/express')
, jade = require('../support/jade')
, app = express.createServer();
// Config
app.register('.jade', jade);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// Middleware
app.use(express.favicon());
app.use(express.logger({ format: '\x1b[90m:remote-addr\x1b[0m - \x1b[33m:method\x1b[0m :url :status \x1b[90m:response-timems\x1b[0m' }));
app.use(app.router);
app.use(express.errorHandler({ showStack: true }));
// Routes
app.get('/', function(req, res){
res.render('tests');
});
app.listen(3000);
console.log('Test server listening on port %d', app.address().port);

6
test/views/layout.jade

@ -0,0 +1,6 @@
!!!
html
head
title node-canvas
body
!= body

1
test/views/tests.jade

@ -0,0 +1 @@
h1 Tests
Loading…
Cancel
Save