Nodejs搭建一个的本地静态文件服务器的方法

方法一:

第一步:在Nodejs安装目录安装:

npm install connect

第二步:在Nodejs安装目录安装:

npm install serve-static

第三步:新建server.js (可以放在项目里去运行也可以放在Nodejs安装目录下运行):

var connect = require("connect");
var serveStatic = require("serve-static");

var app = connect();
app.use(serveStatic("D:\项目文件夹"));

app.listen(5000);

第四步:

运行node server.js

方法二:使用express框架的示例

1.下载express依赖

npm install express

2.新建server.js (可以放在项目里去运行也可以放在Nodejs安装目录下运行):

//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.运行node server.js

方法三:使用koa框架的示例

1.安装koa koa-static

npm install koa koa-static

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

[email protected]@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

npm 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端口");

以上内容均参考他人

https://segmentfault.com/q/1010000005090969/a-1020000005091099

https://www.cnblogs.com/hl1223/articles/8244111.html

猜你喜欢

转载自blog.csdn.net/weixin_38047955/article/details/82981721