Click here to load reader

Node getting-started

  • View
    1.568

  • Download
    7

Embed Size (px)

DESCRIPTION

 

Text of Node getting-started

  • 1. Node: Getting Started
  • 2. Node Node Express Web
  • 3. Node Node ChromeJavaScript Web
  • 4. Node event-driven non-blocking I/O model single-threaded lightweight and efficient
  • 5. app.js var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); node app.js > Server running at http://127.0.0.1:1337/
  • 6. REPL REPL( repple): Read-Eval-Print-Loop, a Nodejs shell.
  • 7. Node
  • 8. EventEmitter HTTP(S) File System Utilities : util.inherits(constructor, super), util.isArray(object), ... Socket, Stream (global) : process, Buffer,
  • 9. URL URI
  • 10. RESTful Web API REST (Representational State Transfer) HTTP PUT DELETE GET POST Express app.VERB() VERBHTTP app.get(), app.put(). Http Verb Route Intro GET /user/:id Id POST /create PUT /update/:id DELETE /delete/:id
  • 11. MongoDBdocument-oriented storage. MongoDBNoSQL db -> collection -> document Redisadvanced key/value store. Redis /
  • 12. MongoDB MongoDBBSONNode MongoDB 1. MongoDB Native Node.js Driver, MongDBNode 2. Mongoose, ORM(Object Relational Mapping) Mongoose Schema
  • 13. var HobbySchema = new Schema({ name: { type: String, required: true, trim: true } }); var UserSchema = new Schema({ id: { type: Number, required: true, trim: true, unique: true }, name: { type: String, required: true, trim: true }, birthday: Date, hobbies: [HobbySchema] }); var UserModel = model(UserModel, UserSchema); var lily = new UserModel({ id: lily, name: Lily Allen, birthday: new Date(2012, 11, 20), hobbies: ['reading', 'music'] });
  • 14. mongoose mongoose.connect(mongodb://127.0.0.1/mydb); mongoose.connect('mongodb://username:passwor [email protected]:port/database?); mongoose.connection.on(open, function() { console.log(Connected to Mongoose); }); lily.save(function(err, data) { }); UserModel.find({id: 'lily'}, function(err, doc) { }); UserModel.update({id: 'lily'}, function(err, doc) { }); UserModel.remove({id: 'lily'}, function(err, doc) { });
  • 15. Redis Redis /(key/value store). Redis MySQL Redis var client = redis.createClient(); var client = redis.createClient(host, port, options);
  • 16. (Middleware, http://en.wikipedia.org/wiki/Middleware) Connect (http://www.senchalabs.org/connect/)Node HTTPConnect10 favicon, logger, static, bodyParser, methodOverride...
  • 17. socket.io (http://socket.io/) Jade - template engine (http://jade-lang.com/) underscore (https://npmjs.org/package/underscore) url, path, querystring, ...
  • 18. http, path, fs, http.createServer(function(req, res) { // URL // // // // // }.listen('8124')
  • 19. Express Web Express Node, Web http://expressjs.com/ https://github.com/visionmedia/express
  • 20. Express Express
  • 21. 1. (site) > express site (Express 3.1.0) > express myapp create : myapp create : myapp/package.json create : myapp/app.js create : myapp/public create : myapp/routes create : myapp/routes/index.js create : myapp/routes/user.js create : myapp/views create : myapp/views/layout.jade create : myapp/views/index.jade create : myapp/public/stylesheets create : myapp/public/stylesheets/style.css create : myapp/public/javascripts create : myapp/public/images
  • 22. 2. > npm install -d 3. > node app
  • 23. package.json example: { "name": "nodeapp" , "description": "My first Node App." , "version": "0.0.1 , "author": "Madhusudhan Srinivasa (http://madhums.github.com)" , "scripts": { "start": "./node_modules/.bin/nodemon server.js" } , "dependencies": { "express": "latest" , "jade": "latest" , "mongoose": "latest" , "connect-mongo": "latest" } } package.json NPM http://package.json.nodejitsu.com/ http://blog.nodejitsu.com/npm-cheatsheet
  • 24.

Search related