标题app.js
var port = 3298;
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var mine = require('./mine').types;
var connect = require('connect');
var history = require('connect-history-api-fallback');
function access(request, response, next) {
var pathname = url.parse(request.url).pathname;
if (pathname.indexOf("undefined")) {
pathname = pathname.split("undefined").join("");
}
if (pathname=="/") {
pathname = "/index.html";
}
var realPath = path.join("./", pathname);
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
fs.exists(realPath, function(exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write("Request URL " + pathname + " not found on this server.");
response.end();
}
else {
fs.readFile(realPath, "binary", function(err, file){
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.end(err);
}
else {
var contentType = mine[ext] || "text/plain";
response.writeHead(200, {
'Content-Type': contentType
})
response.write(file, "binary");
response.end();
}
});
}
});
}
var app = connect().use(history()).use(access);
http.Server(app).listen(port);
console.log("vue-test serve at port :" + port + '......');
mine.js
exports.types = {
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml",
"woff": "application/x-woff",
"woff2": "application/x-woff2",
"tff": "application/x-font-truetype",
"otf": "application/x-font-opentype",
"eot": "application/vnd.ms-fontobject"
};