NodeJS搭建本地服务器环境
2016-11-18 11:17
417 查看
第一步:创建server.js和mine.js文件
server.js
var http = require('http'),
url = require('url'),
fs = require('fs'),
path = require('path'),
mine = require('./mine').types,
PORT = 3000;
var server = http.createServer(function(request, response){
var pathname = url.parse(request.url).pathname,
realPath = path.join('bi_pc', pathname), //注意这里更改为项目名称bi_pc
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('This request URL ' + pathname + ' was 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();
}
})
}
})
})
server.listen(PORT);
console.log('Server runing 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"
}
第二步:项目文件夹(这里是bi_pc)和这两个js文件放在同级目录
第三步:切换到该目录下,执行node server.js,浏览器访问localhost:3000/index.html
server.js
var http = require('http'),
url = require('url'),
fs = require('fs'),
path = require('path'),
mine = require('./mine').types,
PORT = 3000;
var server = http.createServer(function(request, response){
var pathname = url.parse(request.url).pathname,
realPath = path.join('bi_pc', pathname), //注意这里更改为项目名称bi_pc
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('This request URL ' + pathname + ' was 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();
}
})
}
})
})
server.listen(PORT);
console.log('Server runing 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"
}
第二步:项目文件夹(这里是bi_pc)和这两个js文件放在同级目录
第三步:切换到该目录下,执行node server.js,浏览器访问localhost:3000/index.html
相关文章推荐
- 利用nodeJs anywhere搭建本地服务器环境
- 利用nodeJs anywhere搭建本地服务器环境的方法
- 如何在Mac os X上搭建本地服务器环境
- 3分钟快速搭建nodejs本地服务器运行测试html/js
- nodejs搭建本地服务器并访问文件
- python服务器环境搭建(1)——本地服务器准备
- python服务器环境搭建(1)——本地服务器准备
- mark一篇文章--用nodejs搭建一个本地反向代理环境
- 使用Nodejs快速搭建一个的本地静态文件服务器
- nodejs搭建本地服务器并访问文件的方法
- 使用nodejs搭建本地http服务器——李帅醒博客
- python服务器环境搭建(1)——本地服务器准备
- 腾讯云主机服务器环境搭建好,网站本地可访问,外网无法访问的解决办法
- python服务器环境搭建(1)——本地服务器准备
- 织梦本地服务器环境搭建
- Mac OSX环境下搭建本地服务器
- centos6.5环境DNS-本地DNS主从服务器bind的搭建
- centos6.5环境DNS-本地DNS主从服务器bind的搭建
- python服务器环境搭建(1)——本地服务器准备
- python服务器环境搭建(1)——本地服务器准备