var express = require("express");
var app = express();
//指定静态文件路劲static,可以直接访问static下的文件test.png:http://localhost:8080/test.png
app.use(express.static('static'));
app.get("/",function(req,res){
console.log(__dirname);
res.sendFile(__dirname + "/index.html")
})
var server = app.listen(8080,function(){
var host = server.address().address;
var port = server.address().port;
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})
执行server
浏览器访问
这个时候我又写了一个执行文件test.js放在当前目录,源码如下
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})
然后也执行他,在浏览器中访问效果为
var express = require("express");
var app = express();
app.use(express.static('chuayyqing/static'));
app.get("/",function(req,res){
console.log(__dirname);
res.sendFile(__dirname + "/index.html")
})
var server = app.listen(8080,function(){
var host = server.address().address;
var port = server.address().port;
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})
执行代码