Node Basics

From rbachwiki
Revision as of 23:58, 2 April 2018 by Bacchas (talk | contribs)
Jump to navigation Jump to search

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

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');
    }
});

Nodejs api List

Back To Top- Home - Category