- Tested on macOS
- Brew repository needs to be installed
- Recommended editor for smaller nodejs projects: Visual Studio Code or Atom and for larger projects WebStorm.
Install node and npm. Npm will be needed for installing and managing node modules.
brew install node npm
Generate a package.json
for the node project:
mkdir my-test-project
cd my-test-project/
npm init
Now enter the informations about the project. After that a package.json
file should be generated:
macbookpro:my-test-project phil$ cat package.json
{
"name": "my-test-project",
"version": "1.0.0",
"description": "My little test project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Philip Schmid",
"license": "MIT"
}
This file is responsible for the node js application module dependency management and storing some application specific information.
Basically the easiest way to get new node modules is installing them via npm
from the npmjs registery.
In this example we are gonna install a module called express (a minimalist web framework for nodejs):
npm install express --save
--save
is key! With this parameter npm
adds the module name and version to the package.json
file. This ensures, that the nodejs application dependencies can easily be installed with npm install
in the future.
For the understanding:
- Each nodejs application needs an own
package.json
file which spcifies its dependencies. npm install
searches in an incremental way for node module dependencies specified in thepackage.json
file, which are currently unmet.- The command
npm install
downloads the modules to the relative path./node_modules
. This subfolder should be excluded/ignored from any SCM system. - Its possible to save node modules globaly on the system using the "
-g
" parameter (e.g.npm install MODUL-NAME -g
). However, this is usually not recommended.
The real value of nodejs are the thousands of provided modules which can be used for free. To install and automatically add the module to the packages.json
file, use the following command:
npm install MODUL-NAME --save
Now in the index.js
the installed module can be used:
const xy = require('MODUL-NAME');
Create an entry point for the node js application called index.js
. Content:
const express = require('express')
const http = require('http')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
const appPort = 8080;
http.createServer(app).listen(appPort);
console.log('My little test project is listening on port 8080.')
Nodejs doesn't require a specific directory structure. Usually its common to use a structure something like this:
/models
contains all your ORM models (called Schemas in mongoose)/views
contains your view-templates (using any templating language supported in express)/public
contains all static content (images, style-sheets, client-side JavaScript)/assets/images
contains image files/assets/pdf
contains static pdf files/css
contains style sheets (or compiled output by a css engine)/js
contains client side JavaScript/controllers
contain all your express routes, separated by module/area of your application (note: when using the bootstrapping functionality of express, this folder is called /routes)
Source: stackoverflow.com