我正在编写我的第一个nodejs / expressjs应用程序(在本教程中:nettuts+ )我被这个简单但令人讨厌的错误所困扰。我正在尝试做的很简单,我只是通过在命令行中输入(express --sessions --css less --hogan app)来创建我的应用程序项目,它为我创建了这个结构:
/public
/images
/javascripts
/stylesheets
style.less (It came with a style.less)
/routes
/index.js
/user.js
/views
/index.hjs
/app.js
/package.json
好的,直到现在。
我使用WinLess(因为我使用的是Windows 8)来编译style.less并成功创建了style.css。
但是当我启动服务器(节点应用程序)时 我在命令行和浏览器中遇到了这个错误(就在我尝试访问127.0.0.1:3000/stylesheets/style.css时):
Express
500 TypeError: Arguments to path.join must be strings
at f (path.js:204:15)
at Object.filter (native)
at Object.exports.join (path.js:209:40)
at Object.handle (c:\Users\Welder Lourenço\Desktop\WEB\learningexpress\app\node_modules\less- middleware\lib\middleware.js:128:26)
at next (c:\Users\Welder Lourenço\Desktop\WEB\learningexpress\app\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at c:\Users\Welder Lourenço\Desktop\WEB\learningexpress\app\node_modules\express\node_modules\connect\node_modu les\express-session\index.js:350:9
at Object._onImmediate (c:\Users\Welder Lourenço\Desktop\WEB\learningexpress\app\node_modules\express\node_modules\connect\node_modu les\express-session\session\memory.js:50:9)
at processImmediate [as _immediateCallback] (timers.js:330:15)
在命令行中也会出现此错误:
GET /stylesheets/style.css 500
我真的不知道该怎么做,有人可以帮我吗?在这里,遵循我的代码。
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(process.cwd() + '/public'));
app.use(app.router);
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Expressjs版本:3.5.0 Nodejs版本:0.10.26 操作系统:Windows 8
答案 0 :(得分:3)
我遇到了同样的问题。
尝试替换该行:
app.use(require('less-middleware')({ src: __dirname + '/public' }));
带
app.use(require('less-middleware')(__dirname + '/public'));
概述的格式