Giter Site home page Giter Site logo

node-js-development-mode's Introduction

Node.js is a good peice of software, but it lacks a very critical feature: a "development" mode.
In "development" mode, if you modify your application code, it get applied "on the fly",
without the need to restart the application server.

Development for Node.js under Windows, thus, has been a pain.
You had to go and change a line of code,
then go to the Node console window,
then close it by clicking the cross icon,
then go to Explorer and run "start node.js.bat",
and then go to your web browser and finally hit "Refresh"
just to find out if that modified line of code works.

This script fixes that drawback, and is used to run Node.js in "development" mode (works since version 0.5.10 of Node.js).
Thanks to the Node community for helping me to make it work.
This script supposedly works on Linux / Mac too, since it uses no platform-specific tricks.

Running:
node "c:\work\node-js-development-mode.js" --main-file "code/web/main.js"
or
node "c:\work\node-js-development-mode.js" --main-file "code/web/main.coffee" --coffee-script "c:/work/node/coffee-script/bin/coffee"

You can also specify files, you want to "--watch" (and to "--ignore"), manually (** and * are supported):

node "c:\work\node-js-development-mode.js" --main-file "code/web/main.js" --watch "['*.js', '*.coffee']"

All the files in the "project directory" are scanned at startup.
By default, this directory is '.', but you can change it via "--project_directory".

** means any number of folders, including no folders at all, so '**/test.js' matches 'test.js', 'test/test.js' and 'test/test/test.js'.
* means any number of characters (in folder or file name), but at least one character must be present. So, '*hat.js' matches 'that.js', but doesn't match 'hat.js' and 'test/that.js'.

You can also pass options to your Node.js application using the --options key.
I pass json options like this: --options "{ \"server\": \"home\" }"
And then in you Node.js application:

function get_launch_options()
{
	var index = process.argv.indexOf('options')
	if (index >= 0)
		return JSON.parse(process.argv[index + 1])
}

console.log(get_launch_options()) // { server: 'home' }

My use case.

I, personally, run Node.js with this script (I just double click it in Explorer), which is placed in the root directory of my project.
"run node.js.bat":

@echo off
title node.js
rem use utf-8 encoding in console output:
chcp 65001
node "c:/work/node-js-development-mode/node-js-development-mode.js" --main-file code/web/main.coffee --coffee-script "node_modules/coffee-script/bin/coffee" --watch "['code/**/*.js', 'code/**/*.coffee']" --options "{ \"server\": \"home\" }"
pause

Remember to pass the "project_directory" argument if you are running this batch file not from the root folder of your project: all matching patterns are applied relative to the root project folder.

Also, make sure you've added path to the "node.exe" file to your system "Path" variable.
(right click "My computer", Properties, blah blah blah, Evironmental variables, find "Path" there, click "Edit", add ";" and path to "node.exe" without trailing slash, "OK")

This script was considerably rewritten by Nikolay Kuchumov ([email protected]).
The base for this script was initially created by DracoBlue (dracoblue.net) for Linux platform, and is part of the Spludo Framework.
Then I came by it on the internets:
http://dracoblue.net/dev/hot-reload-for-nodejs-servers-on-code-change/173/
and adapted it for my OS.
(It's windoze, cause I don't have money to buy a Mac. If you'd like to assist me in buying a Mac, just email me)
Now, it event works on other platforms.
I'm planning to use it as a supervisor on my server.

You might want to check out
https://github.com/kuchumovn/node-js-development-mode
to issue an error report, or to request a feature, or to just get a new version.

script version: 1.0.0
Licensed under the terms of MIT License.

node-js-development-mode's People

Contributors

catamphetamine avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.