多少次我必须调用http模块

How many times do I have to call http modul

本文关键字:http 模块 调用 多少次      更新时间:2023-09-26

我有一个简单的应用程序,有两个自定义模块

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, errorredirect函数的对象。

是的。每个模块都有自己的作用域,其中定义的变量在模块外部不可用,除非将其添加到module.exports中。当你require的东西,你只是创建一个变量,它受所有正常的范围规则。

如果这让您感到困惑,您可能需要阅读Node模块文档。