Catégories : Tous - server - configuration

par Mike Ton Il y a 12 années

1015

Node

The application uses Node.js and the Express framework to create a server that handles CRUD operations. The server is set up to listen on port 3000 and uses Mongoose to connect to a MongoDB database.

Node

Node.js

app.js

//Only listen on $ node app.js
if(!module.parent){}

console.log("Express server listening on port %d", app.address().port)

app.listen(3000);

route
CRUD

// Delete

app.del('/documents/:id.:format?', function(req, res){});

// Notice that Express uses del instead of delete

// Update

app.put('/documents/:id.:format?', function(req, res){});

// Read

app.get('/documents/:id.:format?', function(req, res){});

// Create

app.post('/documents.:format?', function(req, res){});

// List

app.get('/documents.:format', function(req, res){});

res.render('index', {});

locals:{title:'Express'}

config
app.configure(function(){});

appconfigure()

production

development

app.use

(express.staticProvider(__dirname + '/public'))

(app.router);

(express.methodOverride());

RESTful HTTP APIs

forms can use hidden variables that express can interpret as a "real" HTTP method

can't rely on browsers understanding HTTP verbs like DELETE

(express.bodyDecoder());

(express.logger());

app.set

('view engine', 'jade');

('views', __dirname + '/views');

modules
var

Document

=require('./models.js').Document(db);

db

=mongoose.connect('mongodb://localhost/nodepad');

mongoose

=require('mongoose').Mongoose;

app

=express.createServer();

=module.exports

express

= require('express');

concept

conventional
server

issue response

recieves request

Node
nonblocking sockets

mostly idle

open

to push from server

running Javascript outside the browser

event loop archetecture

nonblocking

doesn't wait, moves to next task until call backed

single thread

only one thing happening at once

wrapper around V8

makes more js more useful for the context where people use Node

nonblocking library

databases

filesystem