简体   繁体   中英

How can I get my NodeJS application working on a server?

I've coded an application using NodeJS, ejs, express, peer, and socket.io that I've tested locally and everything seems to be working perfectly. Here's a little bit of the code from my main server.js file:

// initialize the express application
const express = require('express');
const app = express();

// initialize the server
const server = require('http').Server(app);

// initialize socket io for real-time communication with the server
const io = require('socket.io')(server);

// import the peer-to-peer configuration and create the peer server
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
    debug: true
});


// set the engine as an embedded javascript
app.set('view engine', 'ejs');

// set where the public files exist
app.use(express.static('public'));

// set the peer server url
app.use('/peerjs', peerServer);

// generate the front-end index file for the study
app.get('/', (req, res) => {
    res.render('index');
});

So I've recently gained access to a bit of personal server space at work and just wanted to see if I could deploy the application there. I've installed npm, NodeJS, and all the dependencies I need on the Linux server side and have uploaded the code to the server as well, but to be honest, I'm not quite sure where to go from here?

When testing the application locally, I know I need to enable NodeJS with node server.js and then access my index.ejs file by going to localhost:<portnumber>/ on my browser. But I have no clue what the server equivalent is of this? This might be a ridiculous question, but as someone who hasn't worked on the server side of things before, does anyone know how to access the application from the server using my browser or of any web resources that would be good for understanding what's going on here?

Your question isn't ridiculous. It sounds like you've tested thoroughly so far. You may just want to familiarize yourself with testing on Unix/Linux console environment.

A good way to start would be to run the application, then run

ps -la

To make sure the express.js server is running, by looking for your line 'node server.js' in the list.

Next if you want to try having your server, well, serve a request you can use curl. The curl application can request a page using specified port. Here is an example taken from: https://stackoverflow.com/a/35339246/1061018

curl example.com:1234

So you might try

curl http://127.0.0.1:1234 

or

curl http://localhost:1234

It should output the requested page as text.

Run your app the same way on a server

But I have no clue what the server equivalent is of this

There isn't one! The concept of a server is not clear-cut. No machine is really a server, rather, these are just roles we assign to them. Any machine could be a server or a client. Your development machine acts as a server when you run Node - you are serving a web application.

You can run and access your application the exact same way on the linux server you're using at work as you would at home. That being said, there are a few considerations when you'd like to run an application for production versus for development .

Production considerations

You'll likely want to:

  • use standard ports (80 for HTTP, and 443 for HTTPS) so that the user does not have to type in a port number
  • have a domain name so that you can use a name like stackoverflow.com or example.com instead of an IP address
  • obtain a certificate if you want to use HTTPS
  • restart the application if it crashes
  • log any errors

Make your life easier - use a cloud-hosted service

There are a number of online services which handle all of these concerns, and are probably a better fit for someone who does not have much experience. Check out Heroku .

Hosting your application on a private server

If you'd like to go ahead and use a private server, there are a few things you'll need to do:

  • to use standard ports, you'll need to be aware of a few things. Multiple applications cannot share the same port, so if there are several web applications on the server, there's probably a reverse-proxy which provides virtual domain name resolution. Most commonly, this will be Apache or Nginx. In this scenario, your application will listen on port eg 4321, and Apache or Nginx will see incoming requests for example.com on ports 80 and 443 and internally route those to port 4321. Other web applications on the server will each use different internal ports and external domain names. If there are NOT multiple web applications on the server, you'll still need administrative or special web group (privileged) permissions to be able to listen on 80 or 443, so you'll need the cooperation of your server admin.
  • to get a domain name, you can purchase one from an online service like Namecheap (please pick one of the domain name providers that support Net Neutrality and are not generally shitty), or if your server already has a domain name, you can ask your network admin for a subdomain.
  • to get an HTTPS certificate, you can purchase one or use the free service Let's Encrypt. You may need the cooperation of the server admin in order to prove that you own your domain name.
  • to keep the application stable if it crashes, you can use something like PM2 ( Github ), which also provides some monitoring capabilities, or Node Forever . These are fine for a small project. Enterprise-grade solutions are not single-server. You'd almost always host in the cloud, generally using AWS or Google Cloud, with technology like Kubernetes.
  • to log any errors, check out a library/service like Sentry.io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM