User Tools

Site Tools


services:nodeserv

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
services:nodeserv [2024/08/12 13:13] jasservices:nodeserv [2024/08/12 14:01] (current) – removed jas
Line 1: Line 1:
-====== Nodeserve User Documentation ====== 
- 
-Nodeserve allows you to make NodeJS apps available for others to access on the web.  Follow these instructions for enabling access to your NodeJS apps on the web. 
- 
-1.  Create a directory in your home directory called "nodejs" This will be used for hosting your NodeJS applications. 
- 
-  % mkdir ~/nodejs 
- 
-2.  Create a subdirectory inside the "nodejs" directory for each application that you intend to host. 
- 
-For example, if you will be hosting an application "app1", create a directory in your nodejs directory called "app1": 
- 
-  % mkdir ~/nodejs/app1 
- 
-3.  Develop your NodeJS app in its respective app directory. 
- 
-4.  For each app, create a package.json file using the //npm init -y// command. 
- 
-5.  Let the server know that you want your app to be accessible on the web using the "nodeserve" command. 
- 
-The nodeserve command has 3 options - add, del, and list: 
- 
-<code> 
-nodeserve add <appname> to add an application 
-nodeserve del <appname> to delete an application 
-nodeserver list to list applications 
-</code> 
- 
-For example, to let the server know you want "app1" to be accessible on the web: 
- 
-<code> 
-% nodeserve add app1 
-App 'app1' added for user 'jas' with port 4000. 
-</code> 
- 
-You can list the apps that you've made web accessible: 
- 
-<code> 
-% nodeserve list 
- 
-List of apps for user 'jas': 
-  app1 -> Port 4000 
-</code> 
- 
-You can stop an app from being web accessible: 
- 
-<code> 
-% nodeserve del app1 
-App 'app1' deleted for user 'jas'. 
-</code> 
- 
-NOTE: Deleting an app simply tells the Node web server that you don't want it to make your app web accessible.  It doesn't actually delete your app from your home directory. 
-   
-6.  Visit your app on the web by visiting:  https://nodeserve.eecs.yorku.ca/<user>/<app> where <user> is your EECS username, and <app> is your application name.   
- 
-The web server will look up the port number of your app, and set the port number in the PORT environment variable.  It will then  start running your app on the Node web server as your user id.  Since your NodeJS app is running as you, it can read and write files in your home directory.  The Nodeserve web server will automatically forward web traffic from https://nodeserve.eecs.yorku.ca/<user>/<app> to your app.  You can even use query strings like this: https://nodeserve.eecs.yorku.ca/<user>/<app>?opt1=option1&opt2=option2 
- 
-====== Helpful Hints ====== 
- 
-1.  Each user can use the //nodeserve add// command to add up to 10 applications.  If you need to make more apps available, you will need to delete some.  Applications will be disabled at the end of each term. 
- 
-2.  When developing your code, please do not hard-code the port number allocated to you via the nodeserve command in your app. Instead, your code should read the port number from the environment variable //PORT// The system will let your app know which port number to use. 
- 
-For example: 
- 
-<code> 
-const port = process.env.PORT; 
-server.listen(port, () => { 
-    console.log(`Server is listening on port ${port}`); 
-}); 
-</code> 
- 
-3. In order to ensure that the server is not overloaded with applications which do not need to be running all the time, your application will automatically shutdown after approximately 10 minutes of inactivity.  Note however that whenever the application is called again, it will restart. 
- 
-4. Sometimes, you may need to terminate your application before the period of inactivity expires.  For example, if your app is already running, and you make changes to the code, the changes won't be in effect until your app is restarted.    To terminate your app, visit the [[https://nodeserve.eecs.yorku.ca/admin|Nodeserv Admin Page]].  After logging in with your EECS username and password, you'll see a list of your running NodeJS apps with a "Terminate" button beside each.  Click the "Terminate" button beside the app that you wish to terminate, and your app will be immediately terminated.  
- 
-5. Since your application is running on the server, you don't have direct access to view the console, but that doesn't mean that you can't view the console!  To modify your code to ensure that console messages are written to a file called "app.log" in your app directory, add the following to your NodeJS app: 
- 
-<code> 
-const fs = require('fs'); 
-const logFile = fs.createWriteStream(`${__dirname}/app.log`, { flags: 'a' }); 
-const logStdout = process.stdout; 
- 
-// Override console.log to write to the file and stdout 
-console.log = function (...args) { 
-    logFile.write(new Date().toISOString() + ' - ' + args.join(' ') + '\n'); 
-    logStdout.write(new Date().toISOString() + ' - ' + args.join(' ') + '\n'); 
-}; 
-</code> 
  
services/nodeserv.1723482791.txt.gz · Last modified: 2024/08/12 13:13 by jas

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki