Difference between revisions of "Node Basics"

From rbachwiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 7: Line 7:


== Node Modules ==
== Node Modules ==
[https://nodejs.org/api/ Nodejs api List]
<pre>
console.log('starting app');
const fs = require('fs');
const os = require('os');
var user = os.userInfo();
//console.log(user);
fs.appendFile('greet.txt', `hello${user.username}!` , (e)=>{
    if(e){
        console.log('error');
    }
});
</pre>
==Require your Own Files ==
const notes = require('./notes.js');
'''Notes.js file '''
<pre>
module.exports.addNote = (a, b)=>{
var c = a + b;
return c;
};
</pre>
''' Call the file in the app.js file'''
<pre>
console.log('starting app');
const fs = require('fs');
const os = require('os');
const notes = require('./notes.js');
var user = os.userInfo();
var ret = notes.addNote(5,255);
//  console.log(user);
fs.appendFile('greet.txt', `hello${user.username}! you are ${ret}.` , (e)=>{
    if(e){
        console.log('error');
    }
});
</pre>
=== Use external packages in your project===
Go into the project directory
npm init
// answer questions. his will create a json file
''' Installing lodash '''
Lodash is a set of handy utilities
npm install lodash --save
// the --save flag will update the content of the json file
[https://lodash.com/ Lodash API]
===To reload your project with all the necessary modules===
npm install
// this will look in the package json file and reload all necessary modules
=== Install NODEMON  to automatically reload your app ===
npm install nodemon -g
=== Install Yargs for parsing ===
npm install yargs@4.7.1 --save


[https://nodejs.org/api/ Nodejs api List]
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[NodeJS|Category]]==
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[NodeJS|Category]]==

Latest revision as of 19:38, 3 April 2018

Run a Node App

terminal

node filename.js

You can execute node code in the terminal by typing

node 

This will give you a node command prompt

Node Modules

Nodejs api List

console.log('starting app');

const fs = require('fs');
const os = require('os');
 var user = os.userInfo();
 //console.log(user);
fs.appendFile('greet.txt', `hello${user.username}!` , (e)=>{
    if(e){
        console.log('error');
    }
});

Require your Own Files

const notes = require('./notes.js');

Notes.js file

module.exports.addNote = (a, b)=>{
var c = a + b;
return c;
};

Call the file in the app.js file


console.log('starting app');

const fs = require('fs');
const os = require('os');
const notes = require('./notes.js');
 var user = os.userInfo();
 var ret = notes.addNote(5,255);
//  console.log(user);
fs.appendFile('greet.txt', `hello${user.username}! you are ${ret}.` , (e)=>{
    if(e){
        console.log('error');
    }
});


Use external packages in your project

Go into the project directory

npm init
// answer questions. his will create a json file

Installing lodash Lodash is a set of handy utilities

npm install lodash --save
// the --save flag will update the content of the json file

Lodash API

To reload your project with all the necessary modules

npm install
// this will look in the package json file and reload all necessary modules


Install NODEMON to automatically reload your app

npm install nodemon -g

Install Yargs for parsing

npm install yargs@4.7.1 --save


Back To Top- Home - Category