简体   繁体   English

我必须打几次电话

[英]How many times do I have to call http modul

I have a simple app with two custom modules 我有一个带有两个自定义模块的简单应用

app.js: 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: 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: 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;

So when I try to start this example, I get this error: ReferenceError: http is not defined. 因此,当我尝试启动此示例时,出现以下错误:ReferenceError:未定义http。 But i can't understand why. 但是我不明白为什么。 http is required in actions.js Do I have to call it in app.js too? actions.js中需要http是我也必须在app.js中调用它吗? if yes, why ? 如果是,为什么?

You will have to because you didn't export the http module in your actions.js . 您将必须这样做,因为您没有在actions.js导出http模块。

You exported actions which is: 您导出的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();
 }
};

When you say var someModule = require('someModule') you are including whatever was exported in someModule when it does module.exports = ...; 当您说var someModule = require('someModule')您将包括在执行module.exports = ...;时在someModule中导出的任何内容module.exports = ...;

So in app.js your actions var is an object with download , error , and redirect functions. 因此,在app.js您的action var是具有downloaderrorredirect函数的对象。

Yes you do. 是的你是。 Each module has its own scope, and variables defined in them aren't available outside the module unless you add it to module.exports . 每个模块都有自己的作用域,并且在模块中定义的变量在模块外部不可用,除非您将其添加到module.exports When you require something, you are simply creating a variable, and it's subject to all the normal scoping rules. 当您require某些东西时,您只需创建一个变量,它就受所有常规作用域规则的约束。

If this is confusing, you might want to read over the Node module documentation . 如果这令人困惑,则可能需要阅读Node模块文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM