I'm making some frontend experiments and I'd like to have a very basic webserver to quickly start a project and serve the files (one index.html file + some css/js/img files). So I'm trying to make something with node.js and express, I played with both already, but I don't want to use a render engine this time since I'll have only a single static file, with this code I get the html file but not the assets (error 404):
我正在做一些前端实验,我想要一个非常基本的webserver来快速启动一个项目并提供文件(一个索引)。html文件+一些css/js/img文件)。我试着用node做一些东西。js和express,我已经用过了,但这次我不想使用渲染引擎,因为我只有一个静态文件,用这段代码我得到的是html文件,而不是资产(错误404):
var express = require('express'),
app = express.createServer();
app.configure(function(){
app.use(express.static(__dirname + '/static'));
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.listen(3000);
Is there a simple way to do it (in one file if possible) or Express requires the use of a view and render engine ?
是否有一种简单的方法(如果可能的话,在一个文件中)或表示需要使用视图和渲染引擎?
3 个解决方案
#1
13
You could use a solution like this in node.js (link no longer works), as I've blogged about before.
您可以在node中使用这样的解决方案。js (link不再工作),就像我以前在博客中写的那样。
The summarise, install connect with npm install connect
.
总结,安装连接与npm安装连接。
Then paste this code into a file called server.js
in the same folder as your HTML/CSS/JS files.
然后将此代码粘贴到一个名为server的文件中。在与HTML/CSS/ js文件相同的文件夹中。
var util = require('util'),
connect = require('connect'),
port = 1337;
connect.createServer(connect.static(__dirname)).listen(port);
util.puts('Listening on ' + port + '...');
util.puts('Press Ctrl + C to stop.');
Now navigate to that folder in your terminal and run node server.js
, this will give you a temporary web server at http://localhost:1337
现在导航到终端中的文件夹并运行节点服务器。这将在http://localhost:1337为您提供一个临时web服务器
#2
58
I came across this because I have a similar situation. I don't need or like templates. Anything you put in the public/ directory under express gets served as static content (Just like Apache). So I placed my index.html there and used sendfile to handle requests with no file (eg: GET http://mysite/):
我遇到这个是因为我有类似的情况。我不需要或者不喜欢模板。在express中放入公共/目录的任何内容都可以作为静态内容(就像Apache一样)。我把索引放在这里。使用sendfile处理没有文件的请求(例如:GET http://mysite/):
app.get('/', function(req,res) {
res.sendfile('public/index.html');
});
#3
22
Following code worked for me.
下面的代码对我有用。
var express = require('express'),
app = express(),
http = require('http'),
httpServer = http.Server(app);
app.use(express.static(__dirname + '/folder_containing_assets_OR_scripts'));
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.listen(3000);
this loads page with assets
这将加载带有资产的页面
#1
13
You could use a solution like this in node.js (link no longer works), as I've blogged about before.
您可以在node中使用这样的解决方案。js (link不再工作),就像我以前在博客中写的那样。
The summarise, install connect with npm install connect
.
总结,安装连接与npm安装连接。
Then paste this code into a file called server.js
in the same folder as your HTML/CSS/JS files.
然后将此代码粘贴到一个名为server的文件中。在与HTML/CSS/ js文件相同的文件夹中。
var util = require('util'),
connect = require('connect'),
port = 1337;
connect.createServer(connect.static(__dirname)).listen(port);
util.puts('Listening on ' + port + '...');
util.puts('Press Ctrl + C to stop.');
Now navigate to that folder in your terminal and run node server.js
, this will give you a temporary web server at http://localhost:1337
现在导航到终端中的文件夹并运行节点服务器。这将在http://localhost:1337为您提供一个临时web服务器
#2
58
I came across this because I have a similar situation. I don't need or like templates. Anything you put in the public/ directory under express gets served as static content (Just like Apache). So I placed my index.html there and used sendfile to handle requests with no file (eg: GET http://mysite/):
我遇到这个是因为我有类似的情况。我不需要或者不喜欢模板。在express中放入公共/目录的任何内容都可以作为静态内容(就像Apache一样)。我把索引放在这里。使用sendfile处理没有文件的请求(例如:GET http://mysite/):
app.get('/', function(req,res) {
res.sendfile('public/index.html');
});
#3
22
Following code worked for me.
下面的代码对我有用。
var express = require('express'),
app = express(),
http = require('http'),
httpServer = http.Server(app);
app.use(express.static(__dirname + '/folder_containing_assets_OR_scripts'));
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.listen(3000);
this loads page with assets
这将加载带有资产的页面