Skip to content

Instantly share code, notes, and snippets.

@armanhakimsagar
Last active September 20, 2018 11:58
Show Gist options
  • Select an option

  • Save armanhakimsagar/72ad839394a0e030e8dbdf94be9d943c to your computer and use it in GitHub Desktop.

Select an option

Save armanhakimsagar/72ad839394a0e030e8dbdf94be9d943c to your computer and use it in GitHub Desktop.
* Module:
+ nodemon reload, automatically
+ Express 3.x is a light-weight web application framework to help organize your
web application into an MVC architecture on the server side.
+ body parser : its a middleware for parsing JSON
+ express-fileupload : uploading a file
api crud:
https://www.youtube.com/watch?v=4fWWn2Pe2Mk
form crud:
https://scotch.io/courses/build-a-nodejs-website/course-introduction
What is Node Js:
Nodejs is a C++ application.
Because v8 engine written in c++
Node.js is an open source server environment.
Node.js allows you to run JavaScript on the server.
Node.js is an open source server environment.
V8 engine:
Code that we write on our computers is converted or compiled into machine code.
V8 is a powerful open source Javascript engine provided by Google.
The V8 engine is written in C++ and used in Chrome and Nodejs.
v8 engine convert javascripts into machine code.
Node.js is free
Node.js runs on various platforms
Node.js can generate dynamic page content
Node.js can create, open, read, write, delete, and close files on the server
Node.js can collect form data
Node.js can add, delete, modify data in your database
npm is node package manager
download:
download https://nodejs.org/en/ latest feature
Run :
cmd> node -v
cmd> touch filename.js (write console.log("ok"))
cmd> node filename
* Global Object:
buit in objects for work.
all js window object can run in nodejs.
* Clock Project:
var time = 0;
setInterval(function(){
time += 1;
console.log(time+'You have Passed');
},1000);
* Find directory:
console.log(__dirname);
console.log(__filename);
* simple function
function abc(x){
console.log(x);
}
abc(10);
* call back function:
function callBackFunc(mainfun){
mainfun();
}
var mainfun = function(){
console.log("main function");
}
callBackFunc(mainfun);
* http built in module (use port):
var http = require('http');
http.createServer(function (req, res) {
res.write('Hello World!');=
res.end();
}).listen(8080);
hit : http://localhost:8080/
You can set header type:
res.writeHead(200, {'Content-Type': 'text/html'});
you can get uri:
res.write(req.url);
* fs built in module (file handle):
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('a.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(8080);
* file built in create:
fs.open('mynewfile2.txt', 'w', function (err, file) {
if (err) throw err;
console.log('Saved!');
});
* file write:
fs.writeFile('mynewfile2.txt', 'Hello content!', function (err) {
if(err) throw err;
console.log('Saved!');
});
* export built in module:
module is library.
Node.js has a set of built-in modules which you can use without any further installation.
module.exports use for available var in full page.
common.js:
var counter = function (arr){
return "Your array lenght is : "+arr.length;
}
module.exports = counter;
main.js:
var counter = require('./common');
console.log(counter(['a','b','c']));
* multiple exports built in module:
abc.js :
var msg = require('./common');
console.log(msg.counter(['a','b','c']));
console.log(msg.messages("abc"));
common.js:
var counter = function (arr){
return "Your array lenght is : "+arr.length;
};
var messages = function(data){
return data;
};
module.exports.counter = counter;
module.exports.messages = messages;
* external module npm).
Downloading a package is very easy.
Open the command line interface and tell NPM to download the package you want.
I want to download a package called "upper-case":
folder_name > npm init
folder_name > npm install upper-case
var uc = require('upper-case');
res.write(uc("Hello World!"));
* create a form:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}).listen(8080);
* database connection :
cmd > npm install mysql
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: ""
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment