diff --git a/Nodejs-redis-tutorial/app.js b/Nodejs-redis-tutorial/app.js new file mode 100644 index 0000000..d519ffa --- /dev/null +++ b/Nodejs-redis-tutorial/app.js @@ -0,0 +1,67 @@ + +/** + * Module dependencies. + */ + +var express = require('express'); +var routes = require('./routes'); +var user = require('./routes/user'); +var http = require('http'); +var path = require('path'); +var redis = require("redis"), + client = redis.createClient(); +var app = express(); + +// all environments +app.set('port', process.env.PORT || 3000); +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'jade'); +app.use(express.favicon()); +app.use(express.logger('dev')); +app.use(express.json()); +app.use(express.urlencoded()); +app.use(express.methodOverride()); +app.use(app.router); +app.use(express.static(path.join(__dirname, 'public'))); + +// development only +if ('development' == app.get('env')) { + app.use(express.errorHandler()); +} + +app.get('/', routes.index); + +/* +* Error handling if we are unable to connect to redis +*/ + +/* +* Post request handler for add new key +*/ +app.post('/new_key', function(req, res){ + /* + * Error handling if we are unable to connect to redis + */ + client.on("error", function (err) { + res.send("Error occurred"); + }); + client.set(req.body.key, req.body.value, redis.print); + + res.send("Record has been added successfully"); +}); +/* +* Post request handler for get new key +*/ +app.post('/get_key', function(req, res){ + client.on("error", function (err) { + res.send("No such key exist"); + }); + client.get(req.body.key, function(err, reply) { + res.send("The key value = "+reply.toString()); + }); + +}); + +http.createServer(app).listen(app.get('port'), function(){ + console.log('Express server listening on port ' + app.get('port')); +}); diff --git a/Nodejs-redis-tutorial/package.json b/Nodejs-redis-tutorial/package.json new file mode 100644 index 0000000..faa1661 --- /dev/null +++ b/Nodejs-redis-tutorial/package.json @@ -0,0 +1,12 @@ +{ + "name": "application-name", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "express": "3.4.4", + "jade": "*" + } +} \ No newline at end of file diff --git a/Nodejs-redis-tutorial/public/stylesheets/style.css b/Nodejs-redis-tutorial/public/stylesheets/style.css new file mode 100644 index 0000000..30e047d --- /dev/null +++ b/Nodejs-redis-tutorial/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} \ No newline at end of file diff --git a/Nodejs-redis-tutorial/routes/index.js b/Nodejs-redis-tutorial/routes/index.js new file mode 100644 index 0000000..f296005 --- /dev/null +++ b/Nodejs-redis-tutorial/routes/index.js @@ -0,0 +1,8 @@ + +/* + * GET home page. + */ + +exports.index = function(req, res){ + res.render('index', { title: 'Express' }); +}; \ No newline at end of file diff --git a/Nodejs-redis-tutorial/routes/user.js b/Nodejs-redis-tutorial/routes/user.js new file mode 100644 index 0000000..d5b34aa --- /dev/null +++ b/Nodejs-redis-tutorial/routes/user.js @@ -0,0 +1,8 @@ + +/* + * GET users listing. + */ + +exports.list = function(req, res){ + res.send("respond with a resource"); +}; \ No newline at end of file diff --git a/Nodejs-redis-tutorial/views/index.jade b/Nodejs-redis-tutorial/views/index.jade new file mode 100644 index 0000000..99e3219 --- /dev/null +++ b/Nodejs-redis-tutorial/views/index.jade @@ -0,0 +1,28 @@ +extends layout + +block content + h1 string operation using redis +
+ h3 Add new String key (Note : key must be unique) + + form(method="post" , action="/new_key") + p key: + input#title(type="text" , name="key") + p value: + input#body(type="text" , name="value") + p: button(type="submit") Add New Key/value to redis + + h3 update String value (Note : key must Exist, If not a new record will be created) + form(method="POST" , action="/new_key") + p key: + input#title(type="text" , name="key") + p value: + input#body(type="text" , name="value") + p: button(type="submit") Edit value + + h3 Enter key to see what the value stored + form(method="POST" , action="/get_key") + p key: + input#title(type="text" , name="key") + p: button(type="submit") Search Contact + \ No newline at end of file diff --git a/Nodejs-redis-tutorial/views/layout.jade b/Nodejs-redis-tutorial/views/layout.jade new file mode 100644 index 0000000..1b7b305 --- /dev/null +++ b/Nodejs-redis-tutorial/views/layout.jade @@ -0,0 +1,7 @@ +doctype 5 +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + body + block content \ No newline at end of file diff --git a/nodejs-mysql-tutorial/app.js b/nodejs-mysql-tutorial/app.js new file mode 100644 index 0000000..f713ac3 --- /dev/null +++ b/nodejs-mysql-tutorial/app.js @@ -0,0 +1,51 @@ + +//module dependencies +var express = require('express') + , http = require('http') + , mysql = require('mysql') + , path = require('path'); +var app = express(); +// all environments +app.set('port', process.env.PORT || 3002); +app.set('views', __dirname + '/views'); +app.set('view engine', 'jade'); +app.use(express.favicon()); +app.use(express.logger('dev')); +app.use(express.bodyParser()); +app.use(express.methodOverride()); +app.use(app.router); +app.use(express.static(path.join(__dirname, 'public'))); + +app.get('/', function( req, res) { + res.render('index'); +}); +//connect to mysql database +var connection = mysql.createConnection({ + host : 'localhost', + user : 'root', + password : 'yourpasswordgoeshere', + database : 'nodejsmysql' +}); +connection.connect(); +app.get('/users', function (req, res) { + connection.query('select * from nodejs', function(err, docs) { + res.render('users', {users: docs}); + }); +}); +// Add a new User +app.get("/users/new", function (req, res) { + res.render("new"); +}); +// Save the Newly created User +app.post("/users", function (req, res) { + var fname=req.body.fname, + lname=req.body.lname; + connection.query('INSERT INTO nodejs (fname, lname) VALUES (? , ?);', [fname, lname], function(err, docs) { + if (err) res.json(err); + + res.redirect('users'); + }); +}); +http.createServer(app).listen(app.get('port'), function(){ + console.log('Express server listening on port ' + app.get('port')); +}); diff --git a/nodejs-mysql-tutorial/app.js.bak b/nodejs-mysql-tutorial/app.js.bak new file mode 100644 index 0000000..a286d6d --- /dev/null +++ b/nodejs-mysql-tutorial/app.js.bak @@ -0,0 +1,51 @@ + +//module dependencies +var express = require('express') + , http = require('http') + , mysql = require('mysql') + , path = require('path'); +var app = express(); +// all environments +app.set('port', process.env.PORT || 3002); +app.set('views', __dirname + '/views'); +app.set('view engine', 'jade'); +app.use(express.favicon()); +app.use(express.logger('dev')); +app.use(express.bodyParser()); +app.use(express.methodOverride()); +app.use(app.router); +app.use(express.static(path.join(__dirname, 'public'))); + +app.get('/', function( req, res) { + res.render('index'); +}); +//connect to mysql database +var connection = mysql.createConnection({ + host : 'localhost', + user : 'root', + password : 'root', + database : 'nodejsmysql' +}); +connection.connect(); +app.get('/users', function (req, res) { + connection.query('select * from nodejs', function(err, docs) { + res.render('users', {users: docs}); + }); +}); +// Add a new User +app.get("/users/new", function (req, res) { + res.render("new"); +}); +// Save the Newly created User +app.post("/users", function (req, res) { + var fname=req.body.fname, + lname=req.body.lname; + connection.query('INSERT INTO nodejs (fname, lname) VALUES (? , ?);', [fname, lname], function(err, docs) { + if (err) res.json(err); + + res.redirect('users'); + }); +}); +http.createServer(app).listen(app.get('port'), function(){ + console.log('Express server listening on port ' + app.get('port')); +}); diff --git a/nodejs-mysql-tutorial/package.json b/nodejs-mysql-tutorial/package.json new file mode 100644 index 0000000..d171cd6 --- /dev/null +++ b/nodejs-mysql-tutorial/package.json @@ -0,0 +1,12 @@ +{ + "name": "nodejsmongodb", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "nodemon app.js" + }, + "dependencies": { + "express": "3.2.5", + "jade": "*" + } +} \ No newline at end of file diff --git a/nodejs-mysql-tutorial/package.json.bak b/nodejs-mysql-tutorial/package.json.bak new file mode 100644 index 0000000..5b70151 --- /dev/null +++ b/nodejs-mysql-tutorial/package.json.bak @@ -0,0 +1,12 @@ +{ + "name": "helloexpress", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "nodemon app.js" + }, + "dependencies": { + "express": "3.2.5", + "jade": "*" + } +} \ No newline at end of file diff --git a/nodejs-mysql-tutorial/public/stylesheets/style.css b/nodejs-mysql-tutorial/public/stylesheets/style.css new file mode 100644 index 0000000..30e047d --- /dev/null +++ b/nodejs-mysql-tutorial/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} \ No newline at end of file diff --git a/nodejs-mysql-tutorial/routes/index.js b/nodejs-mysql-tutorial/routes/index.js new file mode 100644 index 0000000..f4d7542 --- /dev/null +++ b/nodejs-mysql-tutorial/routes/index.js @@ -0,0 +1,8 @@ + +/* + * GET home page. + */ + +exports.index = function(req, res){ + res.render('index', { title: 'HelloExpress!!!' }); +}; \ No newline at end of file diff --git a/nodejs-mysql-tutorial/routes/user.js b/nodejs-mysql-tutorial/routes/user.js new file mode 100644 index 0000000..d1eb90a --- /dev/null +++ b/nodejs-mysql-tutorial/routes/user.js @@ -0,0 +1,13 @@ + +/* + * GET users listing. + */ + +exports.list = function(req, res){ + res.send("respond with a resource"); +}; + +exports.mist = function(req, res) { + res.render('index',{title: 'HelloExpress'}); +}; + diff --git a/nodejs-mysql-tutorial/views/articles/articles.jade b/nodejs-mysql-tutorial/views/articles/articles.jade new file mode 100644 index 0000000..bd8fb44 --- /dev/null +++ b/nodejs-mysql-tutorial/views/articles/articles.jade @@ -0,0 +1,11 @@ +h1 Showing All Articles + +ul + - each article in articles + li article Title: #{article.title} + li article Body: #{article.body} + hr + +a(href="articles/new") Add New Article +br +a(href="/") Home \ No newline at end of file diff --git a/nodejs-mysql-tutorial/views/articles/edit.jade b/nodejs-mysql-tutorial/views/articles/edit.jade new file mode 100644 index 0000000..eae7105 --- /dev/null +++ b/nodejs-mysql-tutorial/views/articles/edit.jade @@ -0,0 +1,12 @@ +h1 Edit User + +form(method="POST" , action="/users/#{user.name}") + p + input(type="hidden", name="_method" , value="PUT") + p Name: + input#name(type="text" , name="name" , value="#{user.name}") + p Email: + input#email(type="text" , name="email" , value="#{user.email}") + p Age: + input#age(type="number" , name="age" , value="#{user.age}") + p: button(type="submit") Update \ No newline at end of file diff --git a/nodejs-mysql-tutorial/views/articles/index.jade.bak b/nodejs-mysql-tutorial/views/articles/index.jade.bak new file mode 100644 index 0000000..3badcf9 --- /dev/null +++ b/nodejs-mysql-tutorial/views/articles/index.jade.bak @@ -0,0 +1,11 @@ +h1 Showing All Articles + +ul + - each article in articles + li article Title: #{article.title} + li article Body: #{article.body} + hr + +a(href="articles/new") Add New Article +br +a(href="") Home \ No newline at end of file diff --git a/nodejs-mysql-tutorial/views/articles/new.jade b/nodejs-mysql-tutorial/views/articles/new.jade new file mode 100644 index 0000000..f442821 --- /dev/null +++ b/nodejs-mysql-tutorial/views/articles/new.jade @@ -0,0 +1,9 @@ +h1 Add new Article + +form(method="POST" , action="/articles") + p Title: + input#title(type="text" , name="title") + p Body: + input#body(type="textarea" , name="body") + + p: button(type="submit") Add New Article \ No newline at end of file diff --git a/nodejs-mysql-tutorial/views/articles/show.jade b/nodejs-mysql-tutorial/views/articles/show.jade new file mode 100644 index 0000000..169e46d --- /dev/null +++ b/nodejs-mysql-tutorial/views/articles/show.jade @@ -0,0 +1,29 @@ +h1 #{user.name} + +ul + li Email: #{user.email} + li Age: #{user.age} + li Id: #{user._id} + li vi: #{user.__v} + + ul + li: a(href="/users/#{user.name}/edit") Edit + +form(method="POST" , action="/users/#{user.name}") + p + input(type="hidden", name="_method" , value="DELETE") + p: button(type="submit") Delete + + + + + + + + + + + + + + diff --git a/nodejs-mysql-tutorial/views/index.jade b/nodejs-mysql-tutorial/views/index.jade new file mode 100644 index 0000000..752fffa --- /dev/null +++ b/nodejs-mysql-tutorial/views/index.jade @@ -0,0 +1,6 @@ +extends layout + +block content + p Welcome to nodejs mysql tutorial + p + a(href='/users') Show All users \ No newline at end of file diff --git a/nodejs-mysql-tutorial/views/layout.jade b/nodejs-mysql-tutorial/views/layout.jade new file mode 100644 index 0000000..1b7b305 --- /dev/null +++ b/nodejs-mysql-tutorial/views/layout.jade @@ -0,0 +1,7 @@ +doctype 5 +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + body + block content \ No newline at end of file diff --git a/nodejs-mysql-tutorial/views/new.jade b/nodejs-mysql-tutorial/views/new.jade new file mode 100644 index 0000000..7114d7a --- /dev/null +++ b/nodejs-mysql-tutorial/views/new.jade @@ -0,0 +1,9 @@ +h1 Add new Article + +form(method="POST" , action="/users") + p FirstName: + input#title(type="text" , name="fname") + p LastName: + input#body(type="text" , name="lname") + + p: button(type="submit") Add New User \ No newline at end of file diff --git a/nodejs-mysql-tutorial/views/users.jade b/nodejs-mysql-tutorial/views/users.jade new file mode 100644 index 0000000..91f7d5c --- /dev/null +++ b/nodejs-mysql-tutorial/views/users.jade @@ -0,0 +1,11 @@ +h1 Showing All Users + +ul + - each user in users + li User FirstName: #{user.fname} + li User LastName: #{user.lname} + hr + +a(href="users/new") Add New User +br +a(href="/") Home \ No newline at end of file diff --git a/nodejs-pdf-example-tutorial/app.js b/nodejs-pdf-example-tutorial/app.js new file mode 100644 index 0000000..3570e28 --- /dev/null +++ b/nodejs-pdf-example-tutorial/app.js @@ -0,0 +1,93 @@ + +/** + * Module dependencies. + */ + +var express = require('express'); +var routes = require('./routes'); +var http = require('http'); +var path = require('path'); +var fs=require('fs'); +var PDFDocument = require('pdfkit'); + +var app = express(); + +// all environments +app.set('port', process.env.PORT || 3000); +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'jade'); +app.use(express.favicon()); +app.use(express.logger('dev')); +app.use(express.json()); +app.use(express.urlencoded()); +app.use(express.methodOverride()); +app.use(app.router); +app.use(express.static(path.join(__dirname, 'public'))); + +// development only +if ('development' == app.get('env')) { + app.use(express.errorHandler()); +} + +app.get('/', function(req,res){ + doc = new PDFDocument; + doc.fontSize(15); + //Use "\n" for breaking + // Add Randam text to be added in our pdf + lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus.\n'; + lorem +='Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit.\n '; + lorem +=' \n Ut nec accumsan nisl. Suspendisse rhoncus nisl posuere tortor tempus et dapibus elit porta. Cras leo neque, elementum a rhoncus ut, vestibulum non nibh.Phasellus pretium justo turpis. Etiam vulputate, odio vitae tincidunt ultricies, eros odio dapibus nisi, ut tincidunt lacus arcu eu elit. Aenean velit erat, vehicula eget lacinia ut, dignissim non tellus. Aliquam nec lacus mi, sed vestibulum nunc. Suspendisse potenti. Curabitur vitae sem turpis. Vestibulum sed neque eget dolor dapibus porttitor at sit amet sem. Fusce a turpis lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;'; + doc.text(lorem); + //Add a new page for benzier curve and is part of second page + doc.addPage(); + doc.moveTo(100, 200) + .lineTo(100, 160) + .quadraticCurveTo(130, 200, 150, 120) + .bezierCurveTo(190, -40, 200, 200, 300, 150) + .lineTo(400, 90) + .stroke() + //This creates our third page + doc.addPage(); + + grad = doc.moveTo(200, 200).linearGradient(50, 0, 150, 100); + grad.stop(0, 'green').stop(1, 'red'); + doc.rect(50, 0, 100, 100); + doc.fill(grad); + + grad = doc.radialGradient(300, 50, 0, 300, 50, 50); + grad.stop(0, 'orange', 0).stop(1, 'orange', 1) + doc.circle(300, 50, 50); + doc.fill(grad); + //This creates our Fourth page + doc.addPage(); + doc.save() + .moveTo(100, 150) + .lineTo(100, 250) + .lineTo(200, 250) + .fill("#FF3300") + doc.scale(0.6) + .translate(470, -380) + .path('M 250,75 L 323,301 131,161 369,161 177,301 z') + .fill('red', 'even-odd') + .restore() + //This creates our Fifth page + doc.addPage() + .fillColor("blue") + .text('Here is a link!', 100, 100) + .link(100, 100, 160, 27, 'http://google.com/'); + doc.write("output.pdf"); + res.writeHead(200, {"Content-Type": "text/html"}); + res.write('

Pdf is generated blease use below link to download

'); + res.end("Download"); + +}); +app.get('/download', function(req, res){ + //Use the below to send file and promt user to save it to disk + res.download('output.pdf'); + //Use the below to send file and browser will open and display the file + //res.sendfile('output.pdf'); +}); + +http.createServer(app).listen(app.get('port'), function(){ + console.log('Express server listening on port ' + app.get('port')); +}); diff --git a/nodejs-pdf-example-tutorial/output.pdf b/nodejs-pdf-example-tutorial/output.pdf new file mode 100644 index 0000000..bb5bcbd Binary files /dev/null and b/nodejs-pdf-example-tutorial/output.pdf differ diff --git a/nodejs-pdf-example-tutorial/package.json b/nodejs-pdf-example-tutorial/package.json new file mode 100644 index 0000000..19f133d --- /dev/null +++ b/nodejs-pdf-example-tutorial/package.json @@ -0,0 +1,13 @@ +{ + "name": "application-name", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "express": "3.4.4", + "jade": "*", + "pdfkit": "*" + } +} \ No newline at end of file diff --git a/nodejs-pdf-example-tutorial/public/stylesheets/style.css b/nodejs-pdf-example-tutorial/public/stylesheets/style.css new file mode 100644 index 0000000..30e047d --- /dev/null +++ b/nodejs-pdf-example-tutorial/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} \ No newline at end of file diff --git a/nodejs-pdf-example-tutorial/routes/index.js b/nodejs-pdf-example-tutorial/routes/index.js new file mode 100644 index 0000000..f296005 --- /dev/null +++ b/nodejs-pdf-example-tutorial/routes/index.js @@ -0,0 +1,8 @@ + +/* + * GET home page. + */ + +exports.index = function(req, res){ + res.render('index', { title: 'Express' }); +}; \ No newline at end of file diff --git a/nodejs-pdf-example-tutorial/routes/user.js b/nodejs-pdf-example-tutorial/routes/user.js new file mode 100644 index 0000000..d5b34aa --- /dev/null +++ b/nodejs-pdf-example-tutorial/routes/user.js @@ -0,0 +1,8 @@ + +/* + * GET users listing. + */ + +exports.list = function(req, res){ + res.send("respond with a resource"); +}; \ No newline at end of file diff --git a/nodejs-pdf-example-tutorial/views/index.jade b/nodejs-pdf-example-tutorial/views/index.jade new file mode 100644 index 0000000..ef7b09f --- /dev/null +++ b/nodejs-pdf-example-tutorial/views/index.jade @@ -0,0 +1,5 @@ +extends layout + +block content + h1= title + p Welcome to #{title} \ No newline at end of file diff --git a/nodejs-pdf-example-tutorial/views/layout.jade b/nodejs-pdf-example-tutorial/views/layout.jade new file mode 100644 index 0000000..1b7b305 --- /dev/null +++ b/nodejs-pdf-example-tutorial/views/layout.jade @@ -0,0 +1,7 @@ +doctype 5 +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + body + block content \ No newline at end of file diff --git a/nodejs-read-and-write-file/app.js b/nodejs-read-and-write-file/app.js new file mode 100644 index 0000000..fb109c1 --- /dev/null +++ b/nodejs-read-and-write-file/app.js @@ -0,0 +1,80 @@ + +/** + * Module dependencies. + */ + +var express = require('express'); +var routes = require('./routes'); +var user = require('./routes/user'); +var http = require('http'); +var path = require('path'); +var fs=require('fs'); +var app = express(); + +// all environments +app.set('port', process.env.PORT || 3000); +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'jade'); +app.use(express.favicon()); +app.use(express.logger('dev')); +app.use(express.json()); +app.use(express.urlencoded()); +app.use(express.methodOverride()); +app.use(app.router); +app.use(express.static(path.join(__dirname, 'public'))); + +// development only +if ('development' == app.get('env')) { + app.use(express.errorHandler()); +} + +app.get('/', routes.index); +app.post('/readfile',function(req,res){ + fs.readFile(req.body.file_name+'.txt', function (err, data) { + if (err){ + //Error of 34 is thrown if file doesn't exist + if(err.errno==34){ + res.end('No such file exist');; + } + + }else{ + res.end(data); + } + + }); + //The below is the synchronous version of the above + // It should be written in try catch to avoid any error + /* + try{ + var data=fs.readFileSync(req.body.file_name+'.txt'); + res.end(data); + }catch(e){ + res.end("No such file or directory"); + } + */ +}); +app.post('/createfile',function(req,res){ + if(req.body.file_name !="" && req.body.file_data !=""){ + fs.writeFile(req.body.file_name+'.txt', req.body.file_data, function (err) { + if (err){ + res.end('File could not be saved'); + }else{ + res.end('File has been saved'); + } + }); + }else{ + res.end('Please provide file name and or content'); + } + //The below is the synchronous version of the above + // It should be written in try catch to avoid any error + /* try{ + var data=fs.writeFileSync(req.body.file_name+'.txt'); + res.end("Data has been written sucessfully"); + }catch(e){ + res.end("Error occurred"); + } */ +}); + +http.createServer(app).listen(app.get('port'), function(){ + console.log('Express server listening on port ' + app.get('port')); +}); diff --git a/nodejs-read-and-write-file/package.json b/nodejs-read-and-write-file/package.json new file mode 100644 index 0000000..faa1661 --- /dev/null +++ b/nodejs-read-and-write-file/package.json @@ -0,0 +1,12 @@ +{ + "name": "application-name", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "express": "3.4.4", + "jade": "*" + } +} \ No newline at end of file diff --git a/nodejs-read-and-write-file/public/stylesheets/style.css b/nodejs-read-and-write-file/public/stylesheets/style.css new file mode 100644 index 0000000..30e047d --- /dev/null +++ b/nodejs-read-and-write-file/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} \ No newline at end of file diff --git a/nodejs-read-and-write-file/routes/index.js b/nodejs-read-and-write-file/routes/index.js new file mode 100644 index 0000000..f296005 --- /dev/null +++ b/nodejs-read-and-write-file/routes/index.js @@ -0,0 +1,8 @@ + +/* + * GET home page. + */ + +exports.index = function(req, res){ + res.render('index', { title: 'Express' }); +}; \ No newline at end of file diff --git a/nodejs-read-and-write-file/routes/user.js b/nodejs-read-and-write-file/routes/user.js new file mode 100644 index 0000000..d5b34aa --- /dev/null +++ b/nodejs-read-and-write-file/routes/user.js @@ -0,0 +1,8 @@ + +/* + * GET users listing. + */ + +exports.list = function(req, res){ + res.send("respond with a resource"); +}; \ No newline at end of file diff --git a/nodejs-read-and-write-file/views/index.jade b/nodejs-read-and-write-file/views/index.jade new file mode 100644 index 0000000..ef0538a --- /dev/null +++ b/nodejs-read-and-write-file/views/index.jade @@ -0,0 +1,17 @@ +extends layout + +block content + h1 Enter file name and data( a text file will be created and we can read it back by providing the name later + form(method="post" , action="/createfile") + p File Name: + input(type="text" , name="file_name" , id="file_name") + p File content: + textarea(cols="15" , rows="5" , name="file_data" , id="file_data") + p: button(type="submit") Submit +
+ h1 Enter file name if itexist data will be read back + + form(method="post" , action="/readfile") + p File Name: + input(type="text" , name="file_name" , id="file_name") + p: button(type="submit") Submit \ No newline at end of file diff --git a/nodejs-read-and-write-file/views/layout.jade b/nodejs-read-and-write-file/views/layout.jade new file mode 100644 index 0000000..1b7b305 --- /dev/null +++ b/nodejs-read-and-write-file/views/layout.jade @@ -0,0 +1,7 @@ +doctype 5 +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + body + block content \ No newline at end of file diff --git a/nodejs-socketio-tutorial/index.html b/nodejs-socketio-tutorial/index.html new file mode 100644 index 0000000..0beab03 --- /dev/null +++ b/nodejs-socketio-tutorial/index.html @@ -0,0 +1,39 @@ + + + + Chat Application using node.js /socket.io + + + + +
+ +
+ Broadcast Message:
+ + + + + \ No newline at end of file diff --git a/nodejs-socketio-tutorial/index.js b/nodejs-socketio-tutorial/index.js new file mode 100644 index 0000000..3fbb5a9 --- /dev/null +++ b/nodejs-socketio-tutorial/index.js @@ -0,0 +1,27 @@ +var app = require('express')(); +var http = require('http').Server(app); +var socket_io = require('socket.io')(http); + +app.get('/', function(req, res){ + res.sendfile('index.html'); +}); + + + +socket_io.on('connection', function(socket){ + console.log('user Connected'); + socket.on('disconnect', function(){ + console.log('user disconnected'); + }); + socket.on('chat message', function(msg){ + socket_io.emit('chat message', msg); + }); + var i=0; + setInterval(function(){ + socket.broadcast.emit('customechat', "BD : "+(i++)); + },500) +}); + +http.listen(3000, function(){ + console.log('listening on *:3000'); +}); \ No newline at end of file diff --git a/nodejs-socketio-tutorial/package.json b/nodejs-socketio-tutorial/package.json new file mode 100644 index 0000000..c19d5a0 --- /dev/null +++ b/nodejs-socketio-tutorial/package.json @@ -0,0 +1,9 @@ +{ + "name": "socket-chat-example", + "version": "0.0.1", + "description": "my first socket.io app", + "dependencies": { + "express": "^4.4.0", + "socket.io": "^1.0.3" + } +} diff --git a/tls-node/csr.pem b/tls-node/csr.pem new file mode 100644 index 0000000..46b0ef3 --- /dev/null +++ b/tls-node/csr.pem @@ -0,0 +1,13 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIB8DCCAVkCAQAwgZgxCzAJBgNVBAYTAklOMRQwEgYDVQQIDAtNQUhBUkFTSFRS +QTEPMA0GA1UEBwwGTVVNQkFJMRkwFwYDVQQKDBB0dXRvcmlhbGluZHVzdHJ5MQsw +CQYDVQQLDAJJVDESMBAGA1UEAwwJbG9jYWxob3N0MSYwJAYJKoZIhvcNAQkBFhds +b2NhbGhvc3RAbG9jYWxob3N0LmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC +gYEAoshAiI7bUwIgpDxi5dD+cICR190B7ciinCS2GqTp1ruUA5REEc2dGr2l94Vt +TtMDfwSzdIwWsWxjiNXb3fmrzw0RrLzZz5U7H3Tp+aK7gajoioPHDEUuhP04+mgq +Ln1qhMIBWWqsu/2pSa5HFswmnPLvYS7nsBpwS8qJcxzmNsECAwEAAaAXMBUGCSqG +SIb3DQEJBzEIDAZzcGlkZXIwDQYJKoZIhvcNAQEFBQADgYEAetutyHh3tgxdskVn +ViBL5BJs1ldzhuUB2HkVYzPOaHPouWfDiuUdVrbofAIpZl7ssg80aCUK3q+IPSDy +wloJ9dFMWKx+k3mk9AALhoe5xVTLV3kk3QX5tXEdLcMRwDMjrIjJgC0Q8su0Obhx +IRlCaitZyo40MEgmlsEWvq0IvF0= +-----END CERTIFICATE REQUEST----- diff --git a/tls-node/private-key.pem b/tls-node/private-key.pem new file mode 100644 index 0000000..9b0c680 --- /dev/null +++ b/tls-node/private-key.pem @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXgIBAAKBgQCiyECIjttTAiCkPGLl0P5wgJHX3QHtyKKcJLYapOnWu5QDlEQR +zZ0avaX3hW1O0wN/BLN0jBaxbGOI1dvd+avPDRGsvNnPlTsfdOn5oruBqOiKg8cM +RS6E/Tj6aCoufWqEwgFZaqy7/alJrkcWzCac8u9hLuewGnBLyolzHOY2wQIDAQAB +AoGAaKxYQveJtQDjOHjUJ2+SjYk/Pw2QlfS0C6/XoZ5192y7KuoGM2SCJ5XXySh1 +N4Cc+Opu8vy6AvDtuCm8ripD/WOJbd7GbvYsAIMvN6mm17n6W2CEY9dxgcQyD0qQ +aFzfcV+LzpRlbvGLAywL5Pt6RJuGT3oeeH75eoG1RYeDnXkCQQDR02Aj9UvskS6R +ELzvZocp/nYqpY4o5z1mlfwBgUgyHbAHI0GvihCuS26Mj3v48b+1qJQ6VWn/b9EP +87ydfThvAkEAxpqpnJ7jPyP0B46rzS7L9WUSX+lfUyxiu2PCXfZucdbVopkYclLx +WGsO23/VMc5oZlw530L7rn7owj2DS5w7zwJBAI31Fx9cztAvbTTiASFq2Vl0+hdi +SKqtv+HO6V9PSzv0z+oUVVVg8GYrTJC2ZqaHUC+j7R0GOp+B1AyZ12spk+sCQQCK +nG88bHhWHRW4II8viTW4c247mnl5Z0URXDLPuaqjsTkxXoB9af4fo7ie4b0+Ib0j +k8KgW/OkedahmqUEJGppAkEAmkQ0BRQZnJmTJuwwmOHPv1C9M/hjMPe6Dx+hvGBj +gv5NJi9Rp5324UR63WH9BYIBj7qZmVpAn6e/jc15eOgG0Q== +-----END RSA PRIVATE KEY----- diff --git a/tls-node/public-cert.pem b/tls-node/public-cert.pem new file mode 100644 index 0000000..76c56cf --- /dev/null +++ b/tls-node/public-cert.pem @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICqTCCAhICCQDWoOwOdxWQFDANBgkqhkiG9w0BAQUFADCBmDELMAkGA1UEBhMC +SU4xFDASBgNVBAgMC01BSEFSQVNIVFJBMQ8wDQYDVQQHDAZNVU1CQUkxGTAXBgNV +BAoMEHR1dG9yaWFsaW5kdXN0cnkxCzAJBgNVBAsMAklUMRIwEAYDVQQDDAlsb2Nh +bGhvc3QxJjAkBgkqhkiG9w0BCQEWF2xvY2FsaG9zdEBsb2NhbGhvc3QuY29tMB4X +DTE0MDYwMTA3NDAwN1oXDTE0MDcwMTA3NDAwN1owgZgxCzAJBgNVBAYTAklOMRQw +EgYDVQQIDAtNQUhBUkFTSFRSQTEPMA0GA1UEBwwGTVVNQkFJMRkwFwYDVQQKDBB0 +dXRvcmlhbGluZHVzdHJ5MQswCQYDVQQLDAJJVDESMBAGA1UEAwwJbG9jYWxob3N0 +MSYwJAYJKoZIhvcNAQkBFhdsb2NhbGhvc3RAbG9jYWxob3N0LmNvbTCBnzANBgkq +hkiG9w0BAQEFAAOBjQAwgYkCgYEAoshAiI7bUwIgpDxi5dD+cICR190B7ciinCS2 +GqTp1ruUA5REEc2dGr2l94VtTtMDfwSzdIwWsWxjiNXb3fmrzw0RrLzZz5U7H3Tp ++aK7gajoioPHDEUuhP04+mgqLn1qhMIBWWqsu/2pSa5HFswmnPLvYS7nsBpwS8qJ +cxzmNsECAwEAATANBgkqhkiG9w0BAQUFAAOBgQBDlLvl1GhPpyPRQmNcZO0zfa1R +iW4dOgO48juybJdn+9Tweh2mDggajgdmPtBovbwZ0RSv74TiKeOyAbAdLDpzAFxT +ybcvtUcFNsFRIlrLjm/R9G1qJPHvkOA6Wm2qRC2CgZ/IjjOxMyM/Y6ubQOgJXw/8 +4sIDANUEUUjtS+Z7kw== +-----END CERTIFICATE----- diff --git a/tls-node/tls-server.js b/tls-node/tls-server.js new file mode 100644 index 0000000..89c2d16 --- /dev/null +++ b/tls-node/tls-server.js @@ -0,0 +1,30 @@ +var tls = require('tls'); +var fs = require('fs'); +var https = require('https'); + +var options = { + key: fs.readFileSync('private-key.pem'), + cert: fs.readFileSync('public-cert.pem'), + rejectUnauthorized:false +}; + + + +/* +* Using https module +*/ +https.createServer(options, function (req, res) { + res.writeHead(200); + res.end("hello world\n"); +}).listen(8000); + +/* +* Using tls module +*/ +var tls = require('tls'); + +tls.createServer(options, function (cleartextStream) { + cleartextStream.write("welcome!\n"); + cleartextStream.setEncoding('utf8'); + cleartextStream.end(); +}).listen(8080); \ No newline at end of file