我正在运行一个基于Node的Web服务器,以下是其代码:
var restify = require('restify');
var server = restify.createServer();
var quotes = [
{ author : 'Audrey Hepburn', text : "Nothing is impossible, the word itself says 'I'm possible'!"},
{ author : 'Walt Disney', text : "You may not realize it when it happens, but a kick in the teeth may be the best thing in the world for you"},
{ author : 'Unknown', text : "Even the greatest was once a beginner. Don't be afraid to take that first step."},
{ author : 'Neale Donald Walsch', text : "You are afraid to die, and you're afraid to live. What a way to exist."}
];
server.get('/', function(req, res) {
res.json(quotes);
});
server.get('/quote/random', function(req, res) {
var id = Math.floor(Math.random() * quotes.length);
var q = quotes[id];
res.json(q);
});
server.get('/quote/:id', function(req, res) {
if(quotes.length <= req.params.id || req.params.id < 0) {
res.statusCode = 404;
return res.send('Error 404: No quote found');
}
var q = quotes[req.params.id];
res.json(q);
});
server.listen(process.env.PORT || 3011);
然后我想在以下代码中进行一个GET请求
var https = require('http');
/**
* HOW TO Make an HTTP Call - GET
*/
// options for GET
var optionsget = {
host : 'http://localhost',
port : 3010,
path : '/quote/random', // the rest of the url with parameters if needed
method : 'GET' // do GET
};
console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
// do the GET request
var reqGet = https.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
// console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('GET result:\n');
process.stdout.write(d);
console.info('\n\nCall completed');
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
我刚开始学习Node.js,甚至不知道这是正确的方法。 我想测试Express和Restify的性能。我对我编写的服务器代码进行了Apache基准测试,并发现Restify表现更好。因此,我想通过调用远程服务进行更多测试,然后再读写MongoDB。以上代码是我的起点,但是出现了错误。
{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
我是否至少朝着正确的方向前进了?我想要进行的测试方式是什么?为什么restify比express更快?有人能指导我在node/express/backbone和mongodb应用方面的最佳起点教程吗?
process.stdout.alwrite.now(d)
- nerdfiles