Debugging nodes src/http.js with node-inspector - node.js

I'm having some bad luck debugging a node.js application. The problem is that I need to set a breakpoint in a file that isn't loaded at start (when --debug-blk) hits in. The file is http.js from node itself.
So I would like to modify the file to add a debugger; statement, but I'm unable to find this http.js file on my disk? The only place I see it on my disk is in the source distribution of node (lib/http.js). Where is this file located after a make install in node-src, and how can I set breakpoints inside it?

http.js is internal to the node executable. It doesn't exist on disk, and the only way to modify it is to build node from source.
Do this:
var http = require('http');
debugger;
// the rest of your app
Your app will start, load http, and then pause. Now you can use the inspector to set breakpoints inside http. (You won't need --debug-brk since the debugger statement will pause the app before anything really happens.)

Related

Webpack somehow bypasses Express routing completely

I am starting from this excellent tutorial: https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/ and trying to extend it by adding a new page to serve through a new route. However after hours of mucking around I am realizing that somehow create-react-app is doing some weird magic (as mentioned in their docs here):
`create-react-app` configures a Webpack development server to run on `localhost:3000`.
This development server will bundle all static assets located under `client/src/`.
All requests to `localhost:3000` will serve `client/index.html` which will include Webpack's `bundle.js`.
The key quote is "All requests to localhost:3000 will serve client/index.html". I have no idea how this happens. So even though i mess around with routes/index.js:
app.route('/')
.get(function (req, res) {
res.sendFile(bipath.join(__dirname, '../public', 'THISCANBEANYRANDOMFILENAME.html'))
});
it doesnt matter because webpack is somehow directing localhost:3000 to index.html anyway. where and how is it doing this? Bottom line I am trying to modify my routes to serve a new html file and am running into all sorts of filepath issues (yes, even when i use require('path') or sendFile(...,{root: __dirname}).)
So what exactly is going on here and can you give me any hints to help me out?
Edit: this could be from babel as well as webpack - i'm not exactly clear where babel hands off and where webpack starts.
I haven't played around with create-react-app, but it seems like instead of using the default npm start, you could create your own server file and run that.
This looks like a good example.
https://medium.com/#patriciolpezjuri/using-create-react-app-with-react-router-express-js-8fa658bf892d#.6y4rrl61q
Alternatively, if you're looking to have routes used as an api, you could proxy them to a different port like shown in the tutorial you linked.

sails.config.XXXX in app.js for SailsJS Framework

I am using Sails Framework with the newrelic plugin. I am currently trying to register the app based on a flag in production.js/development.js. This is the flag in development.js:
ENABLE_NEWRELIC_NODE_SERVICE: false,
And this is the piece of code in App.js:
if (sails.config.ENABLE_NEWRELIC_NODE_SERVICE){
require('sails-hook-newrelic/register');
}
But it seems that sails.config is not accessible in app.js (I might be wrong). Is there any other way to include conditionality in App.js based on config files?
Thanks!
Short of a Sails specific way of doing this, simply require in the config file:
var config = require('./sails.config') // Check the path is correct
Then use the flag:
if (config.ENABLE_NEWRELIC_NODE_SERVICE){
require('sails-hook-newrelic/register');
}
The sails.config object isn't available until Sails is actually loaded. Looking at the code of sails-hook-newrelic, it's not clear to me why register() needs to be called so early; it seems like it could just happen as part of the hook initialization. But, the code isn't written that way, so you're stuck on that point. Looks like there's a pull request open to fix it. In the meantime, assuming that you want to activate New Relic based on the Node environment, you can just check process.NODE_ENV in your app.js:
if (process.NODE_ENV == 'development') {
require('sails-hook-newrelic/register');
}
Just make sure that you start your app with NODE_ENV=development node app.js.

React isomorphic inconvenience

I'm building isomorphic app using Node.js and ReactJS.
I've met a situation when inside some isomorphic React component (rendered both on client and server sides) I need to include only client-side dependency (some notifications library for example).
What is the best way to have it worked (compiled) both on server/client sides?
P.S: I have few thoughts about possible workarounds something like checking for browser env variables (ex. some typeof window !== 'undefined') etc. but I'm not sure it's a good way to go.
Use the lifecycle method componentDidMount which is not invoked on the server instead of checking if window is undefined.
The "downside" is if your client side dependency enhances an element for example or changes any property of it, it'll get the default property first since it was rendered server side and when componentDidMount runs it'll get changed causing a "blink".
If you are using browserify I often use process.browser which is only true in browserified code.
If you wanted to get fancy and remove server code from the browser instead there is also isbrowser which will do just that.
Another way (webpack or browserify) is to take advantage of the browser field in the package.json. You can make it so that the server requires a noop file and the browser requires a file the exposes the client side api.
I have defined variable in webpack configuration file called process.env.WEBPACK and in my code when i need something like bottstrap js or something else i just write
if(process.env.WEBPACK){
//awesome lib included or scss or whatever
}

All requests via Proxy in Node-Webkit

Is there a way to force Node-webkit to use proxied settings for all requests ? I know there is an API solution using setProxyConfig() but this doesn't work with authenticated proxy, the prompt shows up to login but crash when submitted...
So I tried to use node request module, and it works fine :
var request=require('request');
var proxy = request.defaults({'proxy':'http://login:pwd#proxy:port'});
But now my problem is to tell node-webkit to use this for each request.
Any solutions ?
I'm quite new using node and node-webkit so maybe there is a better way to do that.
Thanks a lot !
You may try
process.env.http_proxy = 'http://login:pwd#proxy:port'
It will work with request lib, also should impact other requests from node-webkit (assets, ajax)
But may be other libraries don't use environment proxy settings. If that doesn't work you can try https://www.npmjs.com/package/global-tunnel
Not sure if this is solved, but i came up with a simple solution:
i wrote a simple wrapper for the request library
https://gist.github.com/jodevsa/7dd9662b8244359fa0d7626ae7c9bd69
all you have to do is ,
Head to $PROJECT_DIR/node_modules/request/
Rename index.js to core.js
Create a new file called index.js
Copy code content from the above gist link to index.js
Change proxyLoc value to you'r preferred proxy
If you decided to disable proxy , just change value of ON variable to false inside index.js
Cheers :D

Prevent RequireJS from Caching Required Scripts on Nodejs

I am using something like airbnb's rendr, i.e. my own implementation of sharing backbone code between client and server, to build full HTML on the server using the same Backbone models, views, collections and templates I am using on the client. My files/modules are defined as requirejs modules, that way I can share them easily on the client.
In development mode, I want requirejs to refetch/reload any modules from disc when I refresh my browser (without restarting the server), so I get my server rendering uses the newest templates and javascript to finally serve me the newest HTML.
when using requirejs on the server with nodejs, the trick of appending bust parameters to the urlArgs like the following doesn't work, i.e. the server doesn't reload/refetch any modules from disc
urlArgs: "bust=v2"
I wonder, if reloading/refetching requirejs modules from disc-space without restarting the server is possible in node? specifically, that would be very useful for the require-text plugin for template. additionally, it would be nice to apply reloading only to a restricted set of modules.
I've never had to do this, but a quick search shows a few options, including automating the server restart:
nodemon
node-supervisor
You may also be able to do something similarly creative to this:
delete require.cache['/home/shimin/test2.js']
You could almost definitely clear the version RequireJS has in cache, forcing it to reload it, although I suspect the node would just serve up the old file again.
Finally, maybe take a look at hot-reloading, which seems to work around the caching without needing to restart the server (example).
Like the OP, I was looking for a way to make development in node with RequireJS easier and faster. In my case, I am using RequireJS with a grunt watch task in node, and wanted to be able to force RequireJS to always get the latest version of a module from the file system. And happily, I found a very simple solution!
I had to slightly modify Henning Kvinnesland’s suggestion by adding the timeout, but now it is working like a charm:
var requirejs = require('requirejs');
// Set up requirejs.config here...
// Disable caching of modules
requirejs.onResourceLoad = function(context, map) {
setTimeout(function() {
requirejs.undef(map.name);
}, 1);
};

Resources