Intercept outgoing HTTP requests to npmjs.org, etc - node.js

I am looking to create a locally running Node.js server that can intercept outgoing requests to the NPM registry (npmjs.org, or whatever).
Is that what a reverse proxy is?
Basically what I would like to do is create a locally running NPM registry - this is for use with locally running Docker containers. Normally I would just use npm link for local development. But when testing libraries using Docker locally, using npm link for that becomes hard to impossible
I know there are some libraries out there that set up a local NPM registry, but I am looking to do this from scratch so I can set it up for my use case. Essentially I want to tarball packages that exist on my filesystem each time an npm install request comes in.
I started experimenting with:
npm config set registry http://localhost:3440
but when I subsequently did an npm install, it didn't seem to hit my locally running server, just went to registry.npmjs.org like normal.
I also tried modifying /etc/hosts
before:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
after:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
registry.npmjs.org localhost:3440
but my local server listening on port 3440 did not seem to intercept any traffic.

That's not a reverse proxy, but that's a forward proxy. And you'll effectively have to use npm config set proxy to use it from npm call.
The main tool used for such proxy is usually squid proxy. And I'm pretty sure you can build more robust and feature-full service using some well known forward proxy tools instead of a custom nodejs tool.
Then you'll have to check where this service is running (on the same docker? on another docker? on your host?). The address you set for npm, in npm config set proxy must be resolvable from inside the container running npm (but it may differ between commands running on the Dockerfile step and commands running in a bash session after that build).

Related

How does a react app can be set up on server

I'm trying to understand what needs to be done to put my react app online.
Until now, I launched it on my mac using npm start, and accessing localhost:3000 or http://127.0.0.1:3000.
So I currently have bought a small server, installed everything (last version of node and npm, git and other necessary things), cloned my repo, and installed all dependencies.
When I do npm start on the server, it says it's available on port 3000. But when I go in my server's ip with the following :3000, it times out.
I don't really understand what need to be done to do this, I found some things about configuring apache on the server, others about using pm2 so have a node script running even after leaving the terminal, but that would be my next step I guess.. And other about configuring things with express (but do I need node+ express here ? As it's a simple front end react page ?).
if you are using webpack devserver, use it for development only
The tools in this guide are only meant for development, please avoid using them in production!
back to your question, there is a difference between binding to 127.0.0.1 or binding to 0.0.0.0
try changing the devserver to listen to 0.0.0.0
webpack.config.js
module.exports = {
//...
devServer: {
host: '0.0.0.0'
}
};
Usage via the CLI
webpack-dev-server --host 0.0.0.0
also note, that you will need to allow ingress rules (incoming connections). that is, allow a request from the internet to reach your server
There are a lot of configurations you will have to do when you deploy your application on a server. Building the app, Nginx, pm2 and even ssl certification. This video is 20min and has all you need. https://www.youtube.com/watch?v=oykl1Ih9pMg&t=1s

How can I run Laravel mix on custom URL?

I am creating one application that needs to be made on staging server from one point. Because creating it on local is impossible as it has some endpoints that other servers in network has to access.
I have created an application in Vue.js and Laravel. In local, I used to run npm run hot so that I don't have to re-compile when I change some code. But as I have to continue developing this application on a live server, I want to run npm run hot on a custom domain like staging.something.com instead of localhost:8080.
I don't have any issue if somehow localhost:8080 co-operates but when I run npm run hot on a live server, Here is the error I get when I try to access the web application.
GET http://localhost:8080//js/app.js net::ERR_CONNECTION_REFUSED
I think it should show IP address of my server instead of localhost. I don't know what's wrong with this but it's not working.
Laravel mix uses the webpack-dev-server package for the run hot command.
webpack-dev-server has a switch --useLocalIp which will make it use the servers local IP address.
It also has a --host switch which can be used to set the IP address manually --host 0.0.0.0

Access watson retrieve and rank behind a proxy in Node

I need to know if the R&R service can be used behind a proxy.
Take a look at the next part to see my investigations.
The code I'm using was working at my home. (No firewall/No Proxy, only a router)
I started from the following code.
https://console.eu-gb.bluemix.net/catalog/starters/personality-insights-nodejs-web-starter
I try to execute it locally and it was working using the following configuration to be able to install the modules.
npm config set registry http://registry.npmjs.org
npm config set proxy http://x.x.x.x:xx
npm config set https-proxy http://x.x.x.x:xx
npm config set strict-ssl false
set http_proxy=http://x.x.x.x:xx
set https_proxy=http://x.x.x.x:xx
set HTTP_PROXY=http://x.x.x.x:xx
set HTTPS_PROXY=http://x.x.x.x:xx
As it was working I started playing around with R&R. But I'm only having an error.
Error searching for documents: Error: connect ETIMEDOUT 158.85.132.88:443
Where the ip address corresponds gateway.watsonplatform.net
ping gateway.watsonplatform.net
PING gateway.watsonplatform.net (158.85.132.88) 56(84) bytes of data.
Because of the firewall the ping are blocked
During my investigations I found that when executing my test for the R&R service every time two ICMP request are send.
Probably because of the firewall. But why this service would need to send ICMP packets ?
Just to recap:
The code is working when there is no firewall/no proxy
The personnality Insight service is working behind proxy/firewall
The R&R isn't
EDIT
After some investigations I've found that there was a bug in the Solr-client so it is currently impossible to make it work.
More info here on this github issue.

Getting 404 on sails project when reaching through machine name

I am pretty new to sails module on node.js
I created a basic web-app and it launches good on
http://localhost:8081/test
but when I try to access with my machine name like
http://mymachinename:8081/test
it goes 404 page. I also tried to access from another PC in same domain and same 404 error.
I expect both requests should return 200 response.
Am I missing something?
Here is a little information and some suggestions. Here I am on MAC OS X.
First, yes it should work out of the box:
$ sails new app && cd $_ && npm install
$ sails lift --port 1338
http://127.0.0.1:1338/
http://192.168.0.5:1338/
http://mymachine.local:1338/
are all equivalent (loopback, my local IP, my local machine name respectively).
Try a port scan and see if the port you think it is running on is actually running or else already occupied.
On Mac OS X.
lsof -i :1337
Perhaps try starting on a different port:
sails lift --port 1338 (i did this for the above)
Did you install sails globally? If so, you could try uninstalling and reinstalling:
npm uninstall -g sails
npm install -g sails
If you are still having problems, they are local and very unlikely specific to SailsJS. Do usual troubleshooting for network / port related issues locally etc.
Finally, to gain access for other users to your local machine, i usually use ngrok. Ngrok exposes your localhost to the web - example:
$ npm install ngrok -g
$ ngrok http 1338
See this example usage:
In the above example, the locally running instance of sails at: localhost:1338 is now available on the Internet served at: http://840fa6c4.ngrok.io

Accessing my node.js by others?

I have successfully launched my node.js server, and can access it through http://localhost:3000
How do I allow others see my development server? I do not have external IP.
For quick sharing in development stage there is a special NPM module called localtunnel. Do not use this method in production website due to security concerns.
Using localtunnel after you installed it (npm install -g localtunnel):
lt --port 3000
It shall output you the externally accessible URL after running the command, share this url with your friends.

Resources