多少次我必须调用http模块
How many times do I have to call http modul
我有一个简单的应用程序,有两个自定义模块
app.js:
var mappings = require('./mappings.js');
var actions = require('./actions.js');
http.createServer(function (req, res) {
var alias = req.url.substring(1);
console.log(req.url);
console.log(alias);
var mapping = mappings[alias] || {
action: 'error',
statusCode: 404,
data: 'File not found'
};
actions[mapping.action](res,mapping);
}).listen(3000);
mappings.js:
var mappings = {
'goloroden': {
action: 'redirect',
url: 'http://www.goloroden.de',
type: 'permanent'
},
'polarbear': {
action: 'download',
url: 'http://www.goloroden.de/images/Logo.png',
fileName: 'PolarBear.png',
contentType: 'image/png',
forceDownload: false
},
'portrait': {
action: 'download',
url: 'file://./DSC_1067-2.jpg',
contentType: 'image/jpeg',
forceDownload: false
}
};
module.exports = mappings;
actions.js:
var http = require('http');
var fs = require('fs');
var url = require('url');
var deliverDownload = function (res, mapping, data) {
var contentDisposition =
mapping.forceDownload ? 'attachement' : 'inline';
res.writeHead(data.statusCode, {
'Content-Type': mapping.contentType,
'Content-Disposition': contentDisposition + '; filename=' + mapping.fileName + ';'
});
data.pipe(res);
};
var actions = {
'download': function (res, mapping) {
var options = url.parse(mapping.url);
switch(options.protocol) {
case 'http:':
http.get(url.parse(mapping.url), function (data) {
deliverDownload(res, mapping, data);
});
break;
case 'file:':
var data = fs.createReadStream(options.host + options.path);
data.statusCode = 200;
deliverDownload(res, mapping, data);
break;
}
},
'error': function (res, mapping) {
res.writeHead(mapping.statusCode, {
'Content-Type': 'text/html'
});
res.end(mapping.statusCode + ' ' + mapping.data);
},
'redirect': function (res, mapping) {
var statusCode = mapping.type === 'permanent' ? 301 : 307;
res.writeHead(statusCode, {
'Location': mapping.url
});
res.end();
}
};
module.exports = actions;
所以当我尝试开始这个例子时,我得到这个错误:ReferenceError: http未定义。但我不明白为什么。在actions.js中需要HTTP我也需要在app。js中调用它吗?
您将不得不这样做,因为您没有在actions.js
中导出http模块。
您输出的actions
是:
var actions = {
'download': function (res, mapping) {
var options = url.parse(mapping.url);
switch(options.protocol) {
case 'http:':
http.get(url.parse(mapping.url), function (data) {
deliverDownload(res, mapping, data);
});
break;
case 'file:':
var data = fs.createReadStream(options.host + options.path);
data.statusCode = 200;
deliverDownload(res, mapping, data);
break;
}
},
'error': function (res, mapping) {
res.writeHead(mapping.statusCode, {
'Content-Type': 'text/html'
});
res.end(mapping.statusCode + ' ' + mapping.data);
},
'redirect': function (res, mapping) {
var statusCode = mapping.type === 'permanent' ? 301 : 307;
res.writeHead(statusCode, {
'Location': mapping.url
});
res.end();
}
};
当你说var someModule = require('someModule')
时,你包括someModule
中导出的内容,当module.exports = ...;
导出时
所以在app.js
中,你的actions var是一个具有download
, error
和redirect
函数的对象。
是的。每个模块都有自己的作用域,其中定义的变量在模块外部不可用,除非将其添加到module.exports
中。当你require
的东西,你只是创建一个变量,它受所有正常的范围规则。
如果这让您感到困惑,您可能需要阅读Node模块文档。
相关文章:
- NodeJS请求模块-http上的body.IncomingMessage
- 节点.js集群模块不会在 http 上释放套接字/端口.Server.close().
- 模块名称“;mysql”;尚未为上下文_加载.使用require([])http://requirejs.org/doc
- websocket可以使用http模块,但不能使用express(node.js)
- angularjs自定义模块与$http注入
- 我如何使用“;http”;模块
- http模块(node.js和browserfy的)没有't使用方法PATCH写入请求正文
- 如何使用请求 npm 模块判断 http 请求是否完成
- 火狐插件错误:在 resource://gre/modules/commonjs/http.js 找不到模块“路径”
- 由于以下原因,无法实例化模块 myApp:错误:[$injector:nomod] http://errors.angu
- 使用 NodeJS 的请求模块进行 HTTP 身份验证,返回未定义的正文
- node.js中的请求和http模块有什么区别
- 无法使用节点请求模块进行基本的 HTTP 身份验证
- 使用本地主机/同一主机的http模块获取请求
- 如何从inter.js测试中加载node.js http模块
- 为什么我无法在 Node.js 中加载这个 http 模块
- HTTP模块加载错误Node.js
- 多少次我必须调用http模块
- 使用fs和http模块下载Node.js的zip文件不工作
- 如何在TypeScript中使用Node ' http '模块