- 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 npmGenerate a package.json for the node project:
mkdir my-test-project
cd my-test-project/
npm initNow 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.jsonfile which spcifies its dependencies. npm installsearches in an incremental way for node module dependencies specified in thepackage.jsonfile, which are currently unmet.- The command
npm installdownloads 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 --saveNow 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:
/modelscontains all your ORM models (called Schemas in mongoose)/viewscontains your view-templates (using any templating language supported in express)/publiccontains all static content (images, style-sheets, client-side JavaScript)/assets/imagescontains image files/assets/pdfcontains static pdf files/csscontains style sheets (or compiled output by a css engine)/jscontains client side JavaScript/controllerscontain 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