Starting Node js app - node.js

So I created a node app that uploads pictures and the app works locally, I can upload stuff from all of my home devices and they end up in my designated upload folder. Next thing is to go global, so I moved the app to an FTP server and... I don't know how to start it. I can't go
node server.js
like I do on my PC in cmd, can I? I open my index page but when I upload something I get: Server responded with 0 code. Just like when I open my index.html without starting the node app trough cmd on my PC. I'm a front-end guy and I don't know almost anything about servers and I've searched quite a bit around the internet, but to little avail.

One fairly quick way to get this set up would be to sign up for a virtual server on Amazon using their EC2 server instances. Just choose the basic instance (whichever one is free for the first year) and then install Node and run npm install on your root directory once you have uploaded the files. Also if you are going to want this site accessible with your own domain you will have to set up an elastic IP address also available via Amazon (AWS). Furthermore if you want to have your url accessable via the standard port 80 (meanning that you don't have to type your url:[port number]/path then you might want to look into setting up a reverse proxy using something like nginx.
I know this sounds like a lot and i won't lie to you this is kind of complicated but there is a lot more to getting a node application up an running that you might expect.

Before a node app can run on a server you need to make sure that:
Node is installed
npm install has successfully run / or all dependencies must be transferred to the app directory in the right place
The port of the node server should be reachable, so the routing must be set up correctly.
Also, you can't start a program from an ftp prompt usually.

Related

How to use Nginx to load pages through express router

So I'm building an end to end application (With node.js/mysql back end, react front end, and using the express router), but I'm having trouble setting up a local development server. I don't need it to be accessed from the outside world, just be able to load different pages connecting to the express router. I don't have any dev ops experience for this, so I'm trying to use nginx to point it to the router which I can't figure out. Is there an easier way to do this?
I also need to run this on a windows machine, which just makes everything slightly more complicated
It's not entirely clear from your description how your application is set up and what the role of Nginx is.
So I'll start from the beginning...
Nginx is primarily an HTTP server which can also function as a proxy for HTTP requests. If you've written a Node.js application using Express, you have written an HTTP server which can handle any routes you have set up and can also serve your static assets (ie. HTML pages, images, front-end Javascript, CSS, etc.). In this case, there is no need for Nginx - if you wrote something like the Express "Hello World" app, then you will see a message like "Example app listening on port 3000" and you can connect to your app by visiting http://localhost:3000 in your browser.
That's it - there's literally nothing else to your app and there is no need for Nginx (or any other HTTP server) to run your application.
Now that's not to say that there is no role for Nginx in your application, but it may not be as an HTTP server. One possibility is that you may want to set up Nginx as a proxy, to handle certain routes by sending the requests to your Node application. For example, I set up an application some time ago which uses Nginx to proxy API routes for my application to a Node application and to serve static assets directly. This may be what you have in mind - if it is, you will need to configure different routes in Nginx to serve different things (and unfortunately there's not enough information in your question to give suggestions on this).
As an aside, you're probably going to find this much easier to set up using Linux - perhaps the Windows Linux Subsystem, a virtual machine running Linux, or Docker.
You'll probably want to use
https://github.com/facebook/create-react-app
create-react-app my-app will set up everything you need (webpack, etc.), and then
npm start will start a local development server.
Should work on Windows, but I don't know, because I wouldn't use/recommend Windows ;-)

Run socket.io chat app in a website that's already running on wampserver

I have recently made this socket.io chat app with the help of online videos. But the problem is I modified the code and now I want to integrate it in a bigger website like a social network.
The problem, apparently, is the code can't find the socket.io.js file even though it is in it. I know you would usually run a server from gitbash, but it is already running one so why run gitbash as well?
The exact error is:
GET http://localhost/socket.io/socket.io.js ERR: NET ABORTED
This is causing all the trouble.
Also, any opinions on doing all of this in php. I can do it in php, it's easy.
You're trying to load the file from
http://localhost/socket.io/socket.io.js
HTTP requests directly to localhost will go to localhost port 80 by default, which you probably don't have a server running on.
Change the source of the js file to the port that your app is running on, should look like this:
http://localhost:3000/socket.io/socket.io.js

How to create web based terminal using xterm.js to ssh into a system on local network

I came across this awesome library xterm.js which is also the base for Visual Studio Code's terminal. I have a very general question.
I want to access a machine(ssh into a machine ) on a local network through a web based terminal(which is out of network, may be on a aws server). I was able to do this in a local network successfully but I could not reach to a conclusion to do it from Internet-->local network .
As an example - An aws server running the application on ip 54.123.11.98 which has a GUI with a button to open terminal. I want to open terminal of a local machine which is in a local network somewhere behind some public ip on local ip 192.168.1.7.
Can the above example be achieved using some sort of solutions where i can use xterm.js so that I don't have to go for building a web based terminal? What are the major security concerns I should keep in mind while exposing the terminals this way ?
I was thinking in line with using a fixed intermediate server between AWS and local network ip and use some sort of reverse ssh tunnel process to do this but I am not sure if this is the right way or could there be a more simple/better way to achieve this.
I know digital ocean, google cloud , they all do this but they have to connect to a computer which has public ip while I have a machine in a local network. I don't really want to configure router to do any kind of setup .
After a bit of research here is working code.
Libraries:
1) https://socket.io/
This library is used for transmit package from client to server.
2) https://github.com/staltz/xstream
This library is used for terminal view.
3) https://github.com/mscdex/ssh2
This is the main library which is used for establishing a connection with your remote server.
Step 1: Install Library 3 into your project folder
Step 2: Start from node side create a server.js file for open socket
Step 3:
Connection client socket to node server (both are in local machine)
The tricky logic is how to use socket and ssh2.
On emission of socket you need to trigger an SSH command using the ssh2 library. On response of the ssh2 library (from server) you need to transmit the socket package to the client. That's it.
Click here to find an example.
That example will have these files & folders:
Type Name
------------
FILE server.js
FILE package.json
FOLDER src
FOLDER xtream
First you need to configure your server IP, user and password or cert file on server.js and just run node server.js.
P.S.: Don't forget to run npm install
Let me know if you have any questions!
After some research later I came across this service : https://tmate.io/ which does the job perfectly. Though if you need a web-based terminal of tmate you have to use their ssh servers as a reverse proxy which ideally I was not comfortable with. However, they provide tmate-server which can be used to host your own reverse proxy server but lacks web UI. But to build a system where you have to access a client behind NAT over ssh on web, below are the steps.
Install and configure tmate-server on some cloud machine.
Install tmate on the client side and configure to connect to a cloud machine.
Create a nodejs application using xterm.js(easy because of WebSocket based communication) which connects to your tmate-server and pass commands to the respective client. (Beware of security issues of exposing this application, since you will be passing Linux commands ).
Depending on your use case you might need a small wrapper around tmate client on client-side to start/stop it automatically or via some UI/manual action.
Note: I wrote a small wrapper on client-side as well to start/stop and pass on the required information to an API server (written in nodejs) which then pass on the information to another API which connects the browser to the respective client session. Since we had written this application it included authentication as well as command restrictions of what can be run on terminal. You can customize it a lot.

Why does ec2 instance not display my website? Using nodejs

I am running a m1.microinstance of aws, using CentOS. I downloaded Yeoman, git, npm and all of the dependencies are present. I am trying to run a MEAN stack on this server, so, mongo, express, angular and node. However, when I visit my public DNS, my site gives me this error: "Oops! Google Chrome could not connect to ec2-54-191-0-63.us-west-2.compute.amazonaws.com". On my admin control panel, I see my instance status, and it says it is running. I understand that if I had used apache, the page that displays is in the /var/www/html directory.. So, how do I get a directory similar to apaches, to display my html files, or whatever I would like the public to see? I have my security groups configured, for inbound, to listen to SSH port 22, for everyone, as well as HTTP port 80, for everyone.
Yeoman set up a nice app folder for me, but for some reason it does not display. I thought maybe I was missing a server.js, but that does not seem to have fixed anything when I added it. Any advice? Thanks!
Make sure you are matching the port all the way through - your browsers URL:PORT, the EC2 routing rules and your NodeJS settings. It looks like you might be listening to a port higher than 80 on the server.
As you mentioned in your comment, if you want to listen on a port below 1024 you will need to run the command as a privileged user.
I didn't run node as root on my AWS server, so it was not setting up my nicely built app that Yeoman made for me.
http://www.stackoverflow.com/questions/9164915 was where I realized my mistake. I am new to linux OS soo, I am learning. :)

How to deploy live Node.js server using Heroku

Sorry for confusing title, new to Node.js and Heroku, but trying to quickly pick it up.
I currently own a domain, and my first goal is to have it set up so I go to the domain, it uses Heroku to wake up and run Node.js, run my web.js, and display "Hello world".
I've already gone through this heroku/node.js tutorial, and so I understand how to set up stuff locally, push to the heroku remote, and run my Node.js server with it (am I understanding that correctly?). However, I couldn't find anything for how you actually put your Node.js files onto your external server (so, in this case, my domain), and connect the service of Heroku to those files, allowing my local machine to interact with the node on my server.
If there's any tutorials or pages you'd recommend, I'd appreciate it. Kinda stuff and most likely confused on quite a few things here. Thanks!
Heroku apps have their own git repository. So, you push from your local git directory to heroku's git remote.
Setup:
git remote add heroku <git://yourherokurepourl.git>
and then every time to deploy:
git push heroku
That's all needed to get your node.js files on heroku's server. Heroku follows foreman as process launcher. foreman needs a special file in project root called procfile. procfile has simple unix commands to launch processes in each line :
web : npm install && node app.js
So, when you push your project to heroku's git. It will look for procfile and launch processes defined there. You can place more commands here. Better install foreman on your local/development machine and test using that.
In heroku app settings you can "bind" your www.domain.com address to node app running on heroku's server. You have to do the same in settings of domain provider. The DNS will now route requests to www.domain.com to your app's server IP.
On heroku, configuration lives in environment. A lot many process.env.* are available on heroku. You can locally simulate this by providing .env files to foreman.
finally, in your node.js code make sure you listen on value provided by process.env.PORT.
Connecting servers:
Use Request module to directly call other server urls.
Or, Let server's subscribe and publish to a centralized service bus.
You are describing operating two servers. One on Heroku, one "on your domain". I suspect you haven't made the connection that you can merely get your domain to point to your Heroku server. Contact your domain name provider with the heroku url you are using and they can do this for you.
In effect they will "point" your domain to your Heroku node.js server and from there it will act as you expect.

Resources