是否有Node.js即用工具(与npm一起安装),可以帮助我通过HTTP将文件夹内容作为文件服务器公开。
例如,如果我有
D:\Folder\file.zip
D:\Folder\file2.html
D:\Folder\folder\file-in-folder.jpg
然后从D:\Folder\node node-file-server.js开始我可以通过
http://hostname/file.zip
http://hostname/file2.html
http://hostname/folder/file-in-folder.jpg
为什么我的节点静态文件服务器丢弃请求?参考一些神秘的
标准node.js静态文件服务器
如果没有这样的工具,我应该使用什么框架?
相关:NodeJS中的基本静态文件服务器
我对这一页上的任何答案都没有太多的运气,然而,下面的答案似乎起了作用。
添加包含以下内容的server.js文件:
const express = require('express')
const path = require('path')
const port = process.env.PORT || 3000
const app = express()
// serve static assets normally
app.use(express.static(__dirname + '/dist'))
// handle every other route with index.html, which will contain
// a script tag to your application's JavaScript file(s).
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, 'dist', 'index.html'))
})
app.listen(port)
console.log("server started on port " + port)
还要确保您需要快递。根据您的设置运行yarn add express-save或npm install express-save(我可以推荐yarn,它非常快)。
您可以将dist更改为您提供内容的任何文件夹。对于我的简单项目,我没有从任何文件夹提供服务,所以我只是删除了dist文件名。
然后可以运行node server.js。由于我必须将项目上传到Heroku服务器,我需要将以下内容添加到package.json文件中:
"scripts": {
"start": "node server.js"
}
使用npm安装express:https://expressjs.com/en/starter/installing.html
在index.html的同一级别创建一个名为server.js的文件,内容如下:
var express = require('express');
var server = express();
server.use(express.static(__dirname));
server.listen(8080);
这将加载index.html文件。如果希望指定要加载的html文件,请使用以下语法:
server.use('/', express.static(__dirname + '/myfile.html'));
如果要将其放置在其他位置,请在第三行设置路径:
server.use('/', express.static(__dirname + '/public'));
CD到包含文件的文件夹,然后使用以下命令从控制台运行节点:
node server.js
浏览到localhost:8080
我对这一页上的任何答案都没有太多的运气,然而,下面的答案似乎起了作用。
添加包含以下内容的server.js文件:
const express = require('express')
const path = require('path')
const port = process.env.PORT || 3000
const app = express()
// serve static assets normally
app.use(express.static(__dirname + '/dist'))
// handle every other route with index.html, which will contain
// a script tag to your application's JavaScript file(s).
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, 'dist', 'index.html'))
})
app.listen(port)
console.log("server started on port " + port)
还要确保您需要快递。根据您的设置运行yarn add express-save或npm install express-save(我可以推荐yarn,它非常快)。
您可以将dist更改为您提供内容的任何文件夹。对于我的简单项目,我没有从任何文件夹提供服务,所以我只是删除了dist文件名。
然后可以运行node server.js。由于我必须将项目上传到Heroku服务器,我需要将以下内容添加到package.json文件中:
"scripts": {
"start": "node server.js"
}
单线™ 证明而不是承诺
第一个是http服务器,hs-link
npm i -g http-server // install
hs C:\repos // run with one line?? FTW!!
第二个由ZEIT.co提供-链接
npm i -g serve // install
serve C:\repos // run with one line?? FTW!!
以下是可用选项,如果这有助于您做出决定。
C:\Users\Qwerty>http-server --help
usage: http-server [path] [options]
options:
-p Port to use [8080]
-a Address to use [0.0.0.0]
-d Show directory listings [true]
-i Display autoIndex [true]
-g --gzip Serve gzip files when possible [false]
-e --ext Default file extension if none supplied [none]
-s --silent Suppress log messages from output
--cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header
Optionally provide CORS headers list separated by commas
-o [path] Open browser window after starting the server
-c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.
To disable caching, use -c-1.
-U --utc Use UTC time format in log messages.
-P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com
-S --ssl Enable https.
-C --cert Path to ssl cert file (default: cert.pem).
-K --key Path to ssl key file (default: key.pem).
-r --robots Respond to /robots.txt [User-agent: *\nDisallow: /]
-h --help Print this list and exit.
C:\Users\Qwerty>serve --help
Usage: serve.js [options] [command]
Commands:
help Display help
Options:
-a, --auth Serve behind basic auth
-c, --cache Time in milliseconds for caching files in the browser
-n, --clipless Don't copy address to clipboard (disabled by default)
-C, --cors Setup * CORS headers to allow requests from any origin (disabled by default)
-h, --help Output usage information
-i, --ignore Files and directories to ignore
-o, --open Open local address in browser (disabled by default)
-p, --port Port to listen on (defaults to 5000)
-S, --silent Don't log anything to the console
-s, --single Serve single page applications (sets `-c` to 1 day)
-t, --treeless Don't display statics tree (disabled by default)
-u, --unzipped Disable GZIP compression
-v, --version Output the version number
如果你需要留意变化,请看主持人,感谢曾亨利的回答