使用node或grunt运行静态网站?

时间:2016-06-19 10:49:17

标签: javascript node.js gruntjs

我是node或grunt的新手。我用html,css和bootstrap开发了一个网站。现在我很困惑如何运行这个网站?我应该使用nodejs来提供静态文件还是使用grunt?

我看到的大部分示例都使用grunt-watchgrunt-serve来提供文件。是仅在开发阶段而不是在生产阶段正在观看文件?

我还希望在我托管它之前缩小文件(css,js等)。我来自java背景,我通常使用apache tomcat来部署和运行webapp,但是对于静态站点,我不想使用tomcat服务器。我该怎么做?

注意:我正在尝试在heroku上部署我的项目。

2 个答案:

答案 0 :(得分:-1)

作为T.J. Crowder anwer的补充

提供静态HTML应用

npm install -g http-server
http-server ./index.html

为nodejs app

提供服务
npm install -g forever
forever start server.js

开发静态应用

//grunt or gulp
//both support watchers, livereaload, minifications, bundling
//google it

开发nodejs app

express
hapi
meanjs
...many other

答案 1 :(得分:-2)

终于得到了答案:

使用express js来提供静态文件

使用npm install express --save -dev

然后在index.jsserver.js文件中:

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

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

app.get('/', function(request, response) {
  response.render('public/');
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

因此上面的行app.use(express.static(__dirname + '/public'));将从此文件夹(即公共)提供静态文件,如html,css等

然后在命令提示符下:node index.js将启动服务器