set up node js

Was playing around with node today, some notes for setting up.

To install node, just go to the nodejs site and download.

To run sample from the node site, create the something like yourapp.js file with the content they provide, open cmd or your terminal, navigate to the folder containing the js file. and run

node yourapp.js

Next step is to use express which is a good mvc framework for node.

First, run

npm install express -g 

to install the express gloablly so that we can use it directly in command line.

note: In OSX, if you encounter something like “Please try running this command again as root/Administrator.” when using npm install, 2 ways to solve, 2nd is recommended:

  1. use sudo + yourCommand to achieve. but it is kind of dangerous
  2. use “sudo chown -R $USER /usr/local” to change permissions on your /usr/local directory.

After installation we can use express to create a scaffold project by running command:

express

in your app directory. This would create several folds and files for you.

Then run

npm install 

under the app directory so that all the dependencies could be downloaded.

Done, now you can run the app using node xxx.

Note, any change make to the app, we need to restart the server to see. But we can install nodemon to let it monitor the changes for us and auto reload the project.

Just run npm install nodemon -g and then use nodemon to start project instead of node.

if you need to run nodemon in intellij, add the “/usr/local/bin/nodemon” into the “Node parameters” . see detail here.

Proxy

For those who, like me, are behind a corporate web proxy, setting up Node.js and using npm can be a real pain. I thought that the web proxy settings would be like the rest of the unix world and require me to set the HTTP_PROXY and HTTPS_PROXY environment variables. Although I just cloned the Node repository from Github so they are already setup. What gives?

A little searching and I discover that npm uses a configuration file and it can be added to via the command line npm config set .... The key to getting it right is the spelling of the settings. This has bit me so many times now! Getting npm to work behind a proxy requires setting the proxy andhttps-proxy settings. The key is noticing the - (dash) is not an _ (underscore).

So the full procedure is install Node.js via the installer or source. Open an command prompt or terminal session and run the following commands to configure npm to work with your web proxy. The commands use proxy.company.com as the address and 8080 as the port.

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s