最终目录结构

demo
│ node_modules
└───public
│ │ index.html
│ │ index.css
│ └───index.js
└───server.js

一、使用express框架的示例

1.下载express依赖

cnpm install express

2.server.js代码

//server.js
var express = require('express');
var app = express(); app.use(express.static('public'));//express.static是express提供的内置的中间件用来设置静态文件路径 app.get('/index.htm', function (req, res) {
res.sendFile(__dirname + "/" + "index.htm");
}) var server = app.listen(3000, function () {
console.log("监听3000端口")
})

3.public目录里面的index.html、index.css、index.js (其他几个方法公用这个文件夹的面问资源文件)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>本地服务器</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="index.css"/>
<script src="index.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h3>本地服务器</h3>
</body>
</html>
//index.css
body{
background: #fff000;
}
//index.js
console.log("index.html加载了index.js")

4.运行 

node server.js

二、使用koa框架的示例 

1.安装koa koa-static

cnpm install koa koa-static

注意:koa要求node的版本较高(node v7.6.0+),如果出现如下错误,要升级node

koa-static@4.0.1@koa-static\index.js:39
return async function serve (ctx, next) {
^^^^^^^^
SyntaxError: Unexpected token function

2.server.js代码如下

const Koa = require('koa');
const app = new Koa();
const path = require('path');
const serve = require('koa-static'); const main = serve(path.join(__dirname+'/public'));
app.use(main); app.listen(3001,function(){
console.log("监听3001端口")
});

三、使用fastify框架的示例  

1.安装fastify serve-static

cnpm install fastify serve-static

2.server.js代码如下

const serveStatic = require('serve-static');
const fastify = require('fastify')();
const path = require('path'); fastify.use('/', serveStatic(path.resolve(__dirname, 'public'))); fastify.listen(3002, function () {
console.log("监听3002端口");
})

三、不使用框架的示例

server.js(不需要引入任何第三方依赖)

var url = require("url"),
fs = require("fs"),
http = require("http"),
path = require("path");
http.createServer(function (req, res) {
var pathname = __dirname + url.parse("/public"+req.url).pathname;//资源指向public目录
if (path.extname(pathname) == "") {
pathname += "/";
}
if (pathname.charAt(pathname.length - 1) == "/") {
pathname += "index.html";
}
fs.exists(pathname, function (exists) {
if (exists) {
switch(path.extname(pathname)){
case ".html":
res.writeHead(200, {"Content-Type": "text/html"});
break;
case ".js":
res.writeHead(200, {"Content-Type": "text/javascript"});
break;
case ".css":
res.writeHead(200, {"Content-Type": "text/css"});
break;
case ".gif":
res.writeHead(200, {"Content-Type": "image/gif"});
break;
case ".jpg":
res.writeHead(200, {"Content-Type": "image/jpeg"});
break;
case ".png":
res.writeHead(200, {"Content-Type": "image/png"});
break;
default:
res.writeHead(200, {"Content-Type": "application/octet-stream"});
}
fs.readFile(pathname, function (err, data) {
res.end(data);
});
} else {
res.writeHead(404, {
"Content-Type": "text/html"
});
res.end("<h1>404 Not Found</h1>");
}
});
}).listen(3003);
console.log("监听3003端口");

  

 

最新文章

  1. ASP.NET MVC5 ModelBinder
  2. JavaScript通过元素id和name直接获取元素的方法
  3. 当想mysql某插入有某字段设置了unique且和之前相同时,会报错,并停止运行
  4. Linux快速入门01-基础概念
  5. yum源的相关事项
  6. target不起作用了
  7. Linux之磁盘管理
  8. Swiper之滑块3
  9. windows7怎么共享文件夹
  10. 深入浅出Node.js (附录A) - 安装Node
  11. java离request获取当前从访问完成url至
  12. shell脚本兼容linux/unix与windows/cygwin的基础(注意处理好CR, LF, CR/LF 回车 换行的问题)
  13. Linux shell-grep
  14. Python tkinter调整元件在窗口中的位置与几何布局管理
  15. 20164305 徐广皓 Exp3 免杀原理与实践
  16. Android的Service组件
  17. 论文笔记:Towards Diverse and Natural Image Descriptions via a Conditional GAN
  18. Solr4.7.0连接Oracle
  19. iOS ----------要学习的地方(链接整理)
  20. BZOJ2620 [Usaco2012 Mar]Haybale Restacking

热门文章

  1. OMPL RRTConnet 生成路径和可视化
  2. linux6 x86-64 RPM包安装mysql5.7.20
  3. linux传输文件lrzsz
  4. 《楞严经四种清净明诲》 (转自学佛网:http://www.xuefo.net/nr/article56/559965.html)
  5. 常用OID(SNMP)
  6. linux中查看端口号
  7. 【ARTS】01_41_左耳听风-201900819~201900825
  8. 【VS开发】ClientToScreen 和ScreenToClient 用法
  9. 032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用
  10. 关于工作中.net转java遇到的一个远程调用传递重复参的问题。