Node.js 使用restify服务静态文件

Node.js 使用restify服务静态文件,node.js,restify,Node.js,Restify,我正在学习使用Node.js。目前,我的文件夹结构如下所示: index.html server.js client index.html subs index.html page.html res css style.css img profile.png js page.js jquery.min.js js是我的Web服务器代码。我使用node server.js从命令行运行它。该文件的内容包括: var restify

我正在学习使用Node.js。目前,我的文件夹结构如下所示:

index.html
server.js
client
  index.html
  subs
    index.html
    page.html
res
  css
    style.css
  img
    profile.png
  js
    page.js
    jquery.min.js
js是我的Web服务器代码。我使用
node server.js
从命令行运行它。该文件的内容包括:

var restify = require('restify');

var server = restify.createServer({
    name: 'Test App',
    version: '1.0.0'
});

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.get('/echo/:name', function (req, res, next) {
    res.send(req.params);
    return next();
});

server.listen(2000, function () {
    console.log('%s running on %s', server.name, server.url);
});
如您所见,此服务器依赖于RESTIFY。有人告诉我必须使用RESTIFY。但是,我不知道如何为静态文件提供服务。例如,如何在我的应用程序中为*.html、*.css、*.png和*.js文件提供服务器

谢谢大家!

来自:

但这将搜索
/public/docs/public/
目录中的文件。
如果不想向其追加请求路径,请使用
appendRequestPath:false
选项

我更喜欢在这里使用密钥:

server.get(/\/public\/?.*/, restify.plugins.serveStatic({
    directory: __dirname 
}));
\uuu dirname
的值等于脚本文件目录路径,该路径假定也是一个文件夹,其中为
公共
目录

现在我们将所有
/public/*
URL映射到
/public/
目录


现在还有一个插件:


我最近才遇到这个问题,所以虽然这可能对你没有帮助,但它可能会帮助其他有问题的人

当您将Restify声明为
const Restify=require('Restify'),serveStatic方法将位于plugins对象中,因此使用
restify.serveStatic
将悄悄失败。访问该方法的正确方法是
restify.plugins.serveStatic


您可以在此处找到更新文档:

根据我当前的restify版本(v5.2.0)

serveStatic
已被移动到
插件中,因此代码如下

server.get(
  /\/(.*)?.*/,
  restify.plugins.serveStatic({
    directory: './static',
  })
)
上述语法将为文件夹
static
上的静态文件提供服务。因此,您可以获得静态文件,如
http://yoursite.com/awesome-photo.jpg

出于某种原因,如果您想在如下
http://yoursite.com/assets/awesome-photo.jpg
例如

代码应该重构成这样

server.get(
  /\/assets\/(.*)?.*/,
  restify.plugins.serveStatic({
    directory: `${app_root}/static`,
    appendRequestPath: false
  })
)

上面的选项
appendRequestPath:false
意味着我们不在文件名中包含
资产
路径

server.get('/', function(req, res, next) {
    fs.readFile(__dirname + '/index.html', function (err, data) {
        if (err) {
            next(err);
            return;
        }
        res.setHeader('Content-Type', 'text/html');
        res.writeHead(200);
        res.end(data);
        next();
    });
});
server.get('/\/.*/', restify.plugins.serveStatic({

    directory: __dirname + "/view/",
    default: './home.html' 

   })
);

这就是我在restify中提供静态文件的方式

server.get(/\/public\/docs\/?.*/, restify.plugins.serveStatic({

          directory: __dirname,

          default: 'index.html'

        }));
公共访问路径将是:example.com/public/docs/index.html

来自Restify 7路由,因此如果您希望
/public/stylesheet.css
为文件
/public/stylesheet.css
提供服务,您的代码现在将如下所示:

server.get('/public/*', restify.plugins.serveStatic({
  directory: __dirname,
}))

这是因为Restif7有一个新的(可能更快的)路由后端:

可能重复:在您的情况下,
\uu dirname
会有哪个值?如何导入fs?
server.get(/\/public\/docs\/?.*/, restify.plugins.serveStatic({

          directory: __dirname,

          default: 'index.html'

        }));
server.get('/public/*', restify.plugins.serveStatic({
  directory: __dirname,
}))