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
Next step is to use express which is a good mvc framework for node.
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:
- use sudo + yourCommand to achieve. but it is kind of dangerous
- 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:
in your app directory. This would create several folds and files for you.
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.
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
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
https-proxy settings. The key is noticing the
- (dash) is not an
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