16
Node.js insights Edi Spring from Lambda IT

Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Node.js insightsEdi Spring from Lambda IT

Page 2: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?
Page 3: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Node.js BasicsBasic HTTP Server

var http = require('http');

function handleRequest(request, response){response.end("Hello World");

}

var server = http.createServer(handleRequest);

server.listen(PORT, function(){console.log("Server listening on: http://localhost:%s", PORT);

});

Page 4: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Node.js BasicsBasic HTTP Server:

• Routing?

• URL Parameters?

• Body Parsers?

• View Engine?

Page 5: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Let’s use a node module

Page 6: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Express.js BasicsBasic Express WebServer

var express = require('express');var app = express();

app.get('/', function (req, res) {res.send('Hello World!');

});

var server = app.listen(PORT, function () {console.log(Server listening on http://localhost:%s',PORT);

});

Page 7: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Express.js BasicsExpress.js Server:

Routing

URL Parameters

Body Parsers

View Engine (Jade,Mustache,Dust)

Page 8: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Node.js RESTfull APIMethods

POST, GET, PUT, DELETE == CREATE, READ, UPDATE, DELETE

Setting up Express.js routes

var express = require('express');var app = express();var router = express.Router();

router.get('/api/entity/', service.getAll);router.get('/api/entity/:id', service.getById);router.post('/api/entity/', service.create);router.put('/api/entity/:id', service.update);router.delete('/api/entity/:id', service.delete);

Page 9: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Let’s use an other node module

Page 10: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Mongoose SchemaSetting up the DataService with mongoDB and mongoose

var mongoose = require('mongoose'),Schema = mongoose.Schema;

var TodoSchema = new mongoose.Schema({name: String,completed: Boolean,note: String,updated_at: { type: Date, default: Date.now },

});

var Todo = mongoose.model('Todo', TodoSchema);

mongoose.connect('mongodb://localhost/testDB');

Page 11: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Data ServiceSetting up the DataService with mongoDB and mongoosevar getAll = function(req, res) {Todo.find(function (err, todos) {if(err) { return handleError(res, err); }return res.json(200, todos);

});};

var getById = function(req, res) {Todo.findById(req.params.id, function (err, todo) {if(err) { return handleError(res, err); }if(!todo) { return res.send(404); }return res.json(todo);

});};

var create = function(req, res) {Todo.create(req.body, function(err, todo) {if(err) { return handleError(res, err); }return res.json(201, todo);

});};

Page 12: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Data Service (complete)Setting up the DataService with mongoDB and mongoosevar getAll = function(req, res) {

Todo.find(function (err, todos) {if(err) { return handleError(res, err); }return res.json(200, todos);

});};

var getById = function(req, res) {Todo.findById(req.params.id, function (err, todo) {

if(err) { return handleError(res, err); }if(!todo) { return res.send(404); }return res.json(todo);

});};

var create = function(req, res) {Todo.create(req.body, function(err, todo) {

if(err) { return handleError(res, err); }return res.json(201, todo);

});};

var update = function(req, res) {if(req.body._id) { delete req.body._id; }Todo.findById(req.params.id, function (err, todo) {

if (err) { return handleError(res, err); }if(!thing) { return res.send(404); }var updated = _.merge(todo, req.body);updated.save(function (err) {

if (err) { return handleError(res, err); }return res.json(200, todo);

});});

};

var delete = function(req, res) {Todo.findById(req.params.id, function (err, todo) {

if(err) { return handleError(res, err); }if(!todo) { return res.send(404); }todo.remove(function(err) {

if(err) { return handleError(res, err); }return res.send(204);

});});

};

Page 13: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Node.js RESTful APIRESTful API with Express.js and Mongoose

What’s missing?

• Security

• Compression

• Logging

• Errorhandling

• …

Page 14: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

We need more node modules

Page 15: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

Express.js Middlewarevar express = require('express');var app = express();

// middleware can be a very simple IP loggerapp.use(function (req, res, next) {var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;console.log('Client IP:', ip);next();

});

// loggervar morgan = require('morgan');app.use(morgan('dev'));

// compressionvar compression = require('compression');app.use(compression());

// security, oAuthvar passport = require('passport');app.use(passport.initialize());

app.use(errorHandler()); // Error handler - has to be last

Page 16: Node.js insights · Node.js Basics Basic HTTP Server: •Routing? •URL Parameters? •Body Parsers? •View Engine?

How to start• https://nodejs.org

• https://expressjs.com/

• https://www.mongodb.org

• Yeoman, scaffolding Tool: http://yeoman.io/

• Yeoman generators:• https://github.com/Liam-Williams/generator-node-express-mongo

• https://github.com/DaftMonk/generator-angular-fullstack