Here is the full guide http://expressjs.com/guide.html
1. Static files ..
app.use(express.staticProvider(__dirname + '/www/public'));
2. View files (EJS)
app.set('views', __dirname + '/www/views');
app.set('view engine', 'ejs');
layout.ejs
<html>
<head><title>EJS Layout</title></head>
<body>
<%- body %>
</body>
</html>
hello.js
<h1>Hello</h1>
<h2><%= api %></h2>
3. nodejs application
// Production: NODE_ENV=production node ex_express.js
// Development: NODE_ENV=development node ex_express.js
var express = require('express'),
app = express.createServer();
app.configure(function(){
app.use(express.logger());
app.use(express.methodOverride());
app.use(express.bodyDecoder());
app.use(app.router);
app.use(express.staticProvider(__dirname + '/www/public'));
app.set('views', __dirname + '/www/views');
app.set('view engine', 'ejs');
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Application.
app.get('/user/:id', function(req, res) {
res.render('hello.ejs', {locals: {"api": "user"}});
});
app.post('/load/:mode', function(req, res) {
// console.dir(req);
var body = "<h1>Load</h1>";
body += req.params.mode + "\n"
body += req.param("workers") + "\n"
res.send(body);
});
app.listen(3000);
console.log("Public Dir: " + __dirname + '/public');
console.log("Service Started at " + 3000);
No comments:
Post a Comment