Skip to content

Latest commit

 

History

History
87 lines (64 loc) · 1.88 KB

nodejs.md

File metadata and controls

87 lines (64 loc) · 1.88 KB
title description created updated
NodeJS
Node.js cheatsheet
2020-01-14
2020-01-14

1. How to install dependent packages

// initialize nodejs project
npm init

//Basic syntax
npm install <package-name>

//To install the package present in git
npm install <git remote url>
/* ---------------git remote url-----------
   <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
   ----------------------------------------
   <protocol> can be git, git+ssh, git+http, git+https, or git+file
*/

// To install a package which is in filesystem and filename must contain `.tar` or `.tar.gz` or `.tgz`.
npm install <tarball file>

Arguments

--global or -g : to install the package globally --production : npm will not install modules listed in devDependencies

Examples

npm install express
npm install git+ssh://git@github.com:npm/cli.git#v1.0.10

2. How to execute NodeJS file

node filename.js

3. Modules

const express = require('express'); // to refer the installed package

const route = require('./route.js'); //to load  the module named route.js
const router = express.Router();

module.exports=router;

4. Console

console.log('Your Message'); //prints to stdout
console.error('error message'); //prints to stderr
console.info('Your message'); //same as console.log
console.warn('warning message'); //same as console.error

5. How to connect to mongodb

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';

let db = null;

MongoClient.connect(url, function (err, client) {
    if (err) {
        console.log('db connection error');
    }
    else
        if (!err) {
            console.log("Connected successfully to database");
            db = client.db('dbname');
        }
});