javascript 如何使用 express js 创建一个简单的 html 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24517753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:57:48  来源:igfitidea点击:

How to create a simple html server using express js

javascriptnode.jsexpressfileserver

提问by Zsoca

I'm new in node.js I want to create a simple express.js static file server, but I have some issues. I have been installed express.js 4.2 globally like this:

我是 node.js 的新手我想创建一个简单的 express.js 静态文件服务器,但我有一些问题。我已经像这样全局安装了 express.js 4.2:

npm install  -g express-generator

I have this code in httpsrv.js:

我在 httpsrv.js 中有这个代码:

var http = require('http');
var express = require('express');
var app = express();

app.use('/', express.static(__dirname + '/public'));
app.listen(3000, function() { console.log('listening')});

I'm not sure is it ok I guess it is not enough, but I cant run it it's failed with error: cannot find module 'express'.

我不确定它是否可以我想这还不够,但我无法运行它它失败并出现错误:找不到模块“express”。

I want to create a simple http server which can serve from specific folder("\public" e.g.) and I'm using .html language. I found on the internet a many bullshit, I don't want to use this .jade thing and I don't want to create a empty web app with express etc. I want express.js http server which can operate like Apache and can serve a static html pages first from a specified folder. Can anybody help me on this, suggest a good article which is explain a step by step, because I'm beginner.

我想创建一个简单的 http 服务器,它可以从特定文件夹(例如“\public”)提供服务,并且我使用的是 .html 语言。我在互联网上发现了很多废话,我不想使用这个 .jade 东西,我不想用 express 等创建一个空的 web 应用程序。我想要 express.js http 服务器,它可以像 Apache 一样运行并且可以首先从指定的文件夹提供静态 html 页面。任何人都可以帮助我,建议一篇很好的文章,它是一步一步解释的,因为我是初学者。

回答by Evan Hahn

If you're just trying to serve static files from a directory called "public", you might have luck with an app like this:

如果您只是尝试从名为“public”的目录提供静态文件,那么您可能会遇到这样的应用程序:

var path = require('path');
var express = require('express');

var app = express();

var staticPath = path.join(__dirname, '/public');
app.use(express.static(staticPath));

app.listen(3000, function() {
  console.log('listening');
});

You'll need to make sure Express is installed. You'll probably run npm install express --savein the same directory as the above JavaScript file. Once you're all ready, you'll run node the_name_of_the_file_above.jsto start your server.

您需要确保安装了 Express。您可能会npm install express --save在与上述 JavaScript 文件相同的目录中运行。一切准备就绪后,您将运行node the_name_of_the_file_above.js以启动服务器。

回答by ashishkumar148

first install express module inspite of express-generator

尽管有 express-generator,但首先安装 express 模块

npm install express

try this removing public

试试这个删除公共

var express = require('express');
var app = express();

app.use('/', express.static(__dirname));
app.listen(3000, function() { console.log('listening')});

it is working fine.

它工作正常。

回答by garryp

This problem shouldn't even need any code or frameworks; install http-server from npm, navigate to the folder in the command prompt and run this command:

这个问题甚至不需要任何代码或框架;从npm安装 http-server ,导航到命令提示符中的文件夹并运行以下命令:

http-server

And it will spin up a lightweight http server and serve static content up from the folder immediately which can be viewed using http://localhost

它将启动一个轻量级的 http 服务器并立即从文件夹中提供静态内容,可以使用http://localhost查看