我是一個開發nodeJs應用程序的新手,並使用browsify將它捆綁到一個文件bundle.js中,並且希望將它部署到Nginx中。 我已經給出了參考的index.html內bundle.js(如下圖所示),這我不知道是做正確的方式。 的nginx.conf文件是:用於託管NodeJs應用程序的Nginx服務器
server {
listen 8056;
root html/node_server;
index index.html index.htm;
server_name localhost;
location/{
try_files $uri $uri/ /index.html;
}
}
index.html文件和bundle.js文件都放在HTM \ node_server文件夾中,我試圖打口8056像http://localhost:8056。但顯示錯誤:
Uncaught TypeError: Cannot read property 'prototype' of undefined in the bundle.js : 78869
在瀏覽器控制檯中,但沒有在Nginx錯誤日誌。
bundle.js:78869是
`/**
* Request prototype.
* @public
*/
var req = Object.create(http.IncomingMessage.prototype)`
我的index.html文件是:
<!doctype html>
<html>
<meta charset="utf-8">
<title>DEV</title>
<base href="/">
<body>
<script type="text/javascript" src="bundle.js"></script>
</html>
文件的NodeJS:
var sql = require("mssql");
var express = require('express');
var http = require("http");
var bodyParser = require('body-parser');
var jwt = require('jsonwebtoken');
var app = express();
app.use(bodyParser.json());
app.use(function (req, res, next) {
//Enabling CORS
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
app.get('/',function(req,res){
res.send('Working');
});
Bundle.js的詳細信息:
`/**
* Module dependencies.
* @private
*/
var accepts = require('accepts');
var deprecate = require('depd')('express');
var isIP = require('net').isIP;
var typeis = require('type-is');
var http = require('http');
var fresh = require('fresh');
var parseRange = require('range-parser');
var parse = require('parseurl');
var proxyaddr = require('proxy-addr');
/**
* Request prototype.
* @public
*/
var req = Object.create(http.IncomingMessage.prototype) ---- Error here
/**
* Module exports.
* @public
*/
module.exports = req
/**
* Return request header.
*
* The `Referrer` header field is special-cased,
* both `Referrer` and `Referer` are interchangeable.
*
* Examples:
*
* req.get('Content-Type');
* // => "text/plain"
*
* req.get('content-type');
* // => "text/plain"
*
* req.get('Something');
* // => undefined
*
* Aliased as `req.header()`.
*
* @param {String} name
* @return {String}
* @public
*/
req.get =
req.header = function header(name) {
if (!name) {
throw new TypeError('name argument is required to req.get');
}
if (typeof name !== 'string') {
throw new TypeError('name must be a string to req.get');
}
var lc = name.toLowerCase();
switch (lc) {
case 'referer':
case 'referrer':
return this.headers.referrer
|| this.headers.referer;
default:
return this.headers[lc];
}
};
`
我錯過了什麼嗎? 如果需要更多的細節,請做評論。
嗨Tomahaug,謝謝。請參閱我的nodeJs文件(編輯問題)(實際上是我的角度2應用程序的服務器端)。當我使用快遞時,我認爲我在那裏做了非常錯誤的事情。那麼我們必須使用http服務器而不是express嗎?或者有什麼方法可以使用快遞? – Amal
嗨,我已將上下文添加到編輯答案中的bundle.js文件中的失敗行。 – Amal
嗨阿馬爾,對不起,我遲到的答覆。看起來你正試圖將node.js http庫綁定到瀏覽器 - 這是無法完成的。 http庫是一個服務器端庫,在客戶端(瀏覽器)中沒有任何用處。 捆綁和優化js文件是(一般)僅適用於客戶端的JavaScript,而不是服務器端JavaScript。所以你不需要捆綁任何東西來運行你的Node.js服務器。 這有道理嗎? – tomahaug