1

I am a newbie developing a nodeJs application and has bundled it using browsify to a file bundle.js and want to deploy it in Nginx. I have given the reference to the bundle.js inside the index.html(shown below), which I am not sure is the right way to do. The nginx.conf file is :

server {

    listen 8056;
    root html/node_server;
    index index.html index.htm;
    server_name localhost;
    location / {
        try_files $uri $uri/ /index.html;
    }
    }

The index.html file and bundle.js files are placed in htm\node_server folder and I am trying to hit port 8056 like http://localhost:8056 . But showing error :

Uncaught TypeError: Cannot read property 'prototype' of undefined in the bundle.js : 78869

in the browser console, but nothing in Nginx error log .

bundle.js : 78869 is

`/**
 * Request prototype.
 * @public
 */

var req = Object.create(http.IncomingMessage.prototype)`

My index.html file is :

<!doctype html>
<html>

  <meta charset="utf-8">
  <title>DEV</title>
  <base href="/">

<body>

<script type="text/javascript" src="bundle.js"></script>
</html>

NodeJs file :

   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 in more detail :

`/**
 * 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];
  }
};
`

Am I missing something? If more details are required, kindly do comment.

0

2 Answers 2

2

I deleted my previous answers, as I had misunderstood your problem.

The issue it seems is that the http library is not suited to be browserified. In other words, there is some JavaScript in your bundle.js file that cannot run in a web browser.

Could you give some context to the failing lines in the bundle.js file?

This issue does not seem to have anything to do with your node.js / nginx setup.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Tomahaug, Thank you. Please see my nodeJs file (edited the question)(which actually is the server side of my angular 2 application). I think I did something terribly wrong there as I was using express. So do we have to use http server instead of express? Or Is there a way I can use express?
Hi, I have added the context to the failing lines in the bundle.js file in the answer which is edited.
Hi Amal, sorry for my late reply. It seems that you're trying to bundle the node.js http library to the browser – this cannot be done. The http library is a server-side library and doesn't have any use in the client (browser). Bundling and optimizing js files is (in general) only for client-side JavaScript and not server-side JavaScript. So you do not need to bundle anything to run your Node.js server. Does this make sense?
0

Wouldn't be better for you start node.js app localy on the server and than configure nginx as reverse proxy ? Here is detailed tutorial how to do it: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

1 Comment

Hi, So you mean, I have to start the node.js app on my express server in a system and use nginx as a reverse server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.