gpt4 book ai didi

node.js - 当收到像 Unresponsive script 这样的消息时,如何运行基于 Nodejs 的项目?

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:29 25 4
gpt4 key购买 nike

运行nodejs项目时,出现Unresponsive script这样的消息

我在 git-hub 上有一个基于 angularjs-rickshaw 的项目。它基于nodejs、bower。项目:ngyewch/angular-rickshaw
上述项目演示:DEMO

我想在本地系统上运行上述项目。我成功安装了所有东西(nodejs、npm、bower)。但是当我输入 http://localhost:3000/ 时,我什么也没得到,我是 Nodejs 新手,请帮助我。正确的网址是什么?

[neelabh@localhost angular-rickshaw]$ node server.js 
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Server running at http://localhost:3000/

如果我运行 1,我会收到以下类型的消息。http://localhost:3000/或 2.http://localhost:3000/#/home enter image description here

server.js

'use strict';

var fs =require('fs'); //for image upload file handling

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

var port =3000;
var host ='localhost';
var serverPath ='/';
var staticPath ='/';

//var staticFilePath = __dirname + serverPath;
var path = require('path');
var staticFilePath = path.join(__dirname, serverPath);
// remove trailing slash if present
if(staticFilePath.substr(-1) === '/'){
staticFilePath = staticFilePath.substr(0, staticFilePath.length - 1);
}

app.configure(function(){
// compress static content
app.use(express.compress());
app.use(serverPath, express.static(staticFilePath)); //serve static files

app.use(express.bodyParser()); //for post content / files - not sure if this is actually necessary?
});

//catch all route to serve index.html (main frontend app)
app.get('*', function(req, res){
res.sendfile(staticFilePath + staticPath+ 'index.html');
});

app.listen(3000, function () {
console.log('Server running at http://' + host + ':' + port + '/');
})
//app.listen(port);

//console.log('Server running at http://'+host+':'+port.toString()+'/');

最佳答案

查看https://github.com/ngyewch/angular-rickshaw/blob/gh-pages/server.js , console.log('Server running at http://'+host+':'+port.toString()+'/') 应该是对 listen 的回调称呼。否则,即使服务器未正常启动,console.log 始终会被执行。

正确的做法是:

 app.listen(3000, function () {
console.log('Server running at http://' + host + ':' + port + '/');
});

对于staticFilePath和其他与路径相关的部分,您应该使用path.join:

 var path = require('path');
var staticFilePath = path.join(__dirname, serverPath);

最终,最好将所有静态文件移动到 public 目录并使用 express.static 提供服务。中间件:

'use strict';

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

var port = 3000;

app.use(express.static('public'));

app.listen(port, function () {
console.log('Server running at http://' + host + ':' + port + '/');
});

关于node.js - 当收到像 Unresponsive script 这样的消息时,如何运行基于 Nodejs 的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com