我需要多少次调用http模块

时间:2014-06-25 18:05:10

标签: javascript node.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:

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中调用它?如果有,为什么?

2 个答案:

答案 0 :(得分:0)

您必须这样做,因为您没有导出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时,download

因此,在error中,您的操作var是具有redirect,{{1}}和{{1}}函数的对象。

答案 1 :(得分:0)

是的,你这样做。每个模块都有自己的范围,除非您将其添加到module.exports,否则模块外部定义的变量不可用。当您require某事时,您只是创建一个变量,并且它受所有正常范围规则的约束。

如果这令人困惑,您可能需要阅读Node module documentation