Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

modified: README.md

	modified:   both_example.js
  • Loading branch information...
commit d75cc21225ebff1e122e3daba02d4652813622b8 1 parent bea6eb0
@shimondoodkin authored
View
55 README.md
@@ -1,7 +1,14 @@
# What is node.js auto restart
-It is a way to watch all .js files if they have been changed and to restart nodejs.
+It is a set of examples and a module with the problems solved simply.
It allows easy development and stable production.
+ - Run on boot:
+ To make your program run automaticaly on boot with Upsrart(you need to configure a .sh file and a .conf file)
+ - Auto restart:
+ autoexit module to watch all .js files if they have been changed and to restart nodejs.
+ - A description of how to make your program crash proof from javascript errors. (segfaults still crash)
+
+
## How to use nodejs auto restart:
Copy `nodejs.sh` and `autoexit.js` to root folder of your application
for example to `/var/www`. Copying of `autoexit.js` is optional and it can be included from deps folder
@@ -22,45 +29,65 @@ for example to `/var/www`. Copying of `autoexit.js` is optional and it can be in
-### You might want to use: `try-catch` that will make your applicaiton not crush on errors
+### You might want to use: `try-catch` that will make your applicaiton not crash on errors
try
{
//your code
}
catch(e)
{
- sys.puts(e.stack)
+ console.log(e.stack)
}
+Also after serving one sucsessful request (application is fully loaded) I add an on error error handler:
+
+ if(!app_loaded)
+ {
+ process.on('uncaughtException', function (err) {
+ console.log('Caught exception: ' + err.stack);
+ });
+ app_loaded=true;
+ }
+
+As i like it, I want it to crash on load errors and exit the application but not on application errors.
+
### Example:
require.paths.unshift(__dirname); //make local paths accessible
- var sys = require('sys'),
- http = require('http');
-
+ var http = require('http');
+ var app_loaded=false;
+
http.createServer(function (req, res) {
-
- try
+
+ if(!app_loaded)
{
-
+ process.on('uncaughtException', function (err) {
+ console.log('Caught exception: ' + err.stack);
+ });
+ app_loaded=true;
+ }
+
+ try
+ {
+
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
catch(e)
{
- sys.puts(e.stack);
+ console.log(e.stack);
}
- }).listen(8124, "127.0.0.1");
- sys.puts('Server running at http://127.0.0.1:8124/');
+ }).listen(8124);
+ console.log('Server running at http://127.0.0.1:8124/');
// exit if any js file or template file is changed.
// it is ok because this script encapsualated in a batch while(true);
// so it runs again after it exits.
var autoexit_watch=require('autoexit').watch;
//
- var on_autoexit=function () { sys.puts('bye bye'); }
+ var on_autoexit=function () { console.log('bye bye'); }
autoexit_watch(__dirname,".js", on_autoexit);
//autoexit_watch(__dirname+"/templates",".html", on_autoexit);
@@ -105,7 +132,7 @@ note: the output will not bet visible if logging is configured in node.sh. the o
Yes I also use Nginx as front. (but it is not required). I use it
to let me in the future to change and integrate different servers seemlessly.
It is basicly: nginx<->nodejs as an upstream.
-also i have added php-cgi to nginx to use moadmin.php - mongodb db editor.
+also i have added php-cgi to nginx to use +Rock mongo+ - a mongodb db editor.
also i've added a log.php , log file viewer so don't even need ssh.
View
153 autoreload.js
@@ -1,153 +0,0 @@
-//
-// node.js auto reload module
-// by Shimon Doodkin.
-// license: 2 close BSD.
-//
-
-var path = require('path');
-var fs = require('fs');
-var loadmoduletimer={};
-var trackedfiles={}; exports.trackedfiles=trackedfiles;
-this.path=__dirname;
-// note: there is an upcomming version of node
-// with auto reload modules probably it will be integrated in the near future.
-//
-// also every time you reload a module it does not free the memory of the reviews module.
-// it means that reloading modules sutes fine for development,
-// but do not relay on havy use of it for production.
-
-function loadlater( filename , callback )
-{
- console.log((new Date).toString()+' will load file: '+filename);
-
- return setTimeout(function ()
- {
- console.log((new Date).toString()+' loading file: '+filename);
- fs.readFile(filename, function (err, content)
- {
- if (err) throw err; // need to add better error handling
- try
- {
- var dirname = path.dirname(filename);
- // create wrapper function
- var wrapper = "(function (exports, require, module, __filename, __dirname) { "
- + content
- + "\n});";
- var compiledWrapper = process.compile(wrapper, filename);
- var newmodule={};
- compiledWrapper.apply(newmodule, [newmodule, require, newmodule, filename, dirname]);
-
- callback(newmodule);
- }
- catch(err)
- {
- if (err)
- {
- console.log(err.message);
- console.log(err.stack);
- }
- //if (err) throw err; // need to add better error handling
- }
- });
- }, 500); // sometimes i had a situation when the watchFile callback called while uploading the file and resulted in error.
-} exports.loadlater=loadlater;
-
-
-function watch()
-{
- var watchfilename,filename,callback;
- if(arguments.length==3)
- {
- watchfilename=arguments[0];
- filename=arguments[1];
- callback=arguments[2];
- }
- else
- {
- watchfilename=arguments[0];
- filename=arguments[0];
- callback=arguments[1];
- }
- if(typeof watchfilename=='string') watchfilename=[watchfilename];
-
- console.log((new Date).toString()+' watch reaload file: '+filename);
- trackedfiles[filename]=true;
- var functionload=function()
- {
- if(loadmoduletimer[filename])
- {
- console.log((new Date).toString()+'timeout cleaned - will load file: '+filename);
- clearTimeout(loadmoduletimer[filename]);
- }
- loadmoduletimer[filename] =
- loadlater( filename ,callback);
- };
- for(var i=0;i<watchfilename.length;i++)
- fs.watchFile(watchfilename[i], functionload);
-};exports.watch=watch;
-
-function watchrel()
-{
- var watchfilename,filename,callback;
- if(arguments.length==3)
- {
- watchfilename=arguments[0];
- filename=arguments[1];
- callback=arguments[2];
- }
- else
- {
- watchfilename=arguments[0];
- filename=arguments[0];
- callback=arguments[1];
- }
- for(var i=0;i<watchfilename.length;i++)
- watchfilename[i]=this.path+'/'+watchfilename[i];
- watch(watchfilename,filename,callback);
-};
-exports.watchrel=watchrel;
-
-
-
-function calllater( filename , callback )
-{
- console.log((new Date).toString()+' will call file: '+filename);
- return setTimeout(function ()
- {
- console.log((new Date).toString()+' calling file: '+filename);
- callback(filename);
- }, 500); // sometimes i had a situation when the watchFile callback called while uploading the file and resulted in error.
-} exports.calllater=calllater;
-
-function watchonly()
-{
- var watchfilename,filename,callback;
- if(arguments.length==3)
- {
- watchfilename=arguments[0];
- filename=arguments[1];
- callback=arguments[2];
- }
- else
- {
- watchfilename=arguments[0];
- filename=arguments[0];
- callback=arguments[1];
- }
- if(typeof watchfilename=='string') watchfilename=[watchfilename];
-
- console.log((new Date).toString()+' watch only file: '+filename);
- trackedfiles[filename]=true;
- var callfunction=function ()
- {
- if(loadmoduletimer[filename])
- {
- console.log((new Date).toString()+'timeout cleaned - will load file: '+filename);
- clearTimeout(loadmoduletimer[filename]);
- }
- loadmoduletimer[filename] =
- calllater( filename ,callback);
- };
- for(var i=0;i<watchfilename.length;i++)
- fs.watchFile(watchfilename[i], callfunction);
-};exports.watchonly=watchonly;
View
29 autoreload_example.js
@@ -1,29 +0,0 @@
-var http = require('http');
-var autoreload= require('./autoreload');
-var mymodule= require('./mymodule_example');
-
-autoreload.watchrel('mymodule_example.js', function (newmodule){
- /* you can put here staff to make your module look like it was initialized well. */
- newmodule.name=mymodule.name;
- //mymodule.init(); // init the module before if possible, it will save error time.
- mymodule=newmodule;
- //mymodule.moreinit(); // while this not finished you may get errors, because of not whell initilized your module.
-});
-
-//autoreload.watchrel('mymodule_example.js', function (newmodule){ mymodule=newmodule; });
-//autoreload.watch(mymodule.filename, function (newmodule){ mymodule=newmodule; }); // might not work if when started the module has errors or filename exports is missing
-
-
-mymodule.name="Shimon";
-
-
-http.createServer(function (request, response) {
- response.writeHead(200, {'Content-Type': 'text/plain'});
- response.end(mymodule.time() + '\n');
-
- //to do, main module reloadable instead of above you might use:
- //mymodule.handlerequest(req,res);
-
-}).listen(8124);
-
-console.log('Server running at http://127.0.0.1:8124/');
View
0  both_example.js 100644 → 100755
File mode changed
View
8 mymodule_example.js
@@ -1,8 +0,0 @@
-console.log('mymodule loaded');
-exports.name='test';
-exports.time=function ()
-{
- // edit the number to see it working
- return " test number 12 name: "+exports.name+" , today: "+(new Date).toString();
-}
-exports.filename=__filename;
Please sign in to comment.
Something went wrong with that request. Please try again.