node入门 express ejs

hello.js

var express = require("express");

var app = express();

app.get("/hello", function(req,res){
res.send("Hello World");
}); var server = app.listen(3000, function(){
console.log("Listening on port %d", server.address().port);
});

hello_html.js

var express = require("express");

var app = express();

app.engine(".html", require("ejs").__express);
app.set("views", __dirname + "/views");
app.set("view engine", "html"); app.get("/hello", function(req,res){
res.send("Hello World");
}); app.get("/", function(req,res){
res.render("index",{"title" : "Hey", "message" : "Hello World!"});
}); var server = app.listen(3000, function(){
console.log("Listening on port %d", server.address().port);
});

views/index.html

<h1><%=title%></h1>
<p><%=message%></p>

==用express起一个server,访问静态资源

var express = require('express');
var path = require('path');
var app = express(); //set static file
app.use(express.static(path.join(__dirname, 'www'))); var server = app.listen(9000,function(){
console.log('server is listening...')
}) //set router
app.get('/',function(req, res){
res.send('Welcome To Express Server!')
})
上一篇:Linux学习 -- 系统管理


下一篇:【Spark工作原理】stage划分原理理解