使用带有express 4的Stylus中间件,SyntaxError

时间:2014-06-25 10:44:38

标签: node.js express stylus

我试图使用nodejs表达v4与stylus,但它抛出SyntaxError。请帮忙,你也可以在下面找到我的server.js ;;

请注意:我第一次创建MEANstack项目,对我不要太苛刻:)

您的环境已设置为使用Node.js 0.10.26(x64)和npm。

Error: Most middleware (like logger) is no longer bundled with Express and must
be installed separately. Please see https://github.com/senchalabs/connect#middle
ware.
    at Function.Object.defineProperty.get (C:\Sites\meanProject\node_modules\exp
ress\lib\express.js:89:13)
    at Object.<anonymous> (C:\Sites\meanProject\server.js:10:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

server.js:

    var express = require('express');
var stylus = require('stylus');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var app = express();

//set view engine
app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());

//style middlware
app.use(stylus.middleware({
  src: __dirname + '/public',
  dest: __dirname + '/public/css',
  compile: function compile(str, path){
     return stylus(str).set('filename', path).set('compress', true);
  }
}));

app.use(express.static(path.join(__dirname + '/public')));//all public req will be responded by public dir now.

//load route
app.get('*', function(req, res){
    res.render('index');
});

//start listening on server
var port = 3000;
app.listen(port);
console.log('Server running at localhost:' + port);

2 个答案:

答案 0 :(得分:2)

@thyforhtian是的,你说得对,我的代码已经过时了。

我修好了。 我不再使用nodejs来编译我的手写笔文件,而是使用gulp。

通过步骤在此处发布我的文件,这对其他人可能会有所帮助。

使用gulp watch编译手写笔文件。和快递服务器js文件的代码

开始安装节点模块

npm init 
npm install -save express jade


// Step 1: Install gulp globally
npm install -g gulp

// Step 2: Install gulp in your project
npm install --save-dev gulp gulp-stylus gulp-plumber

npm install morgan body-parser --save

server.js

var express = require('express');
var stylus = require('stylus');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var app = express();

//set view engine
app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.urlencoded());
app.use(cookieParser());

//all public req will be responded by public dir now.
app.use(express.static(__dirname + '/public'));

//load route
app.get('*', function(req, res){
    res.render('index');
});

//start listening on server
var port = 3000;
app.listen(port);
console.log('Server running at localhost:' + port);

gulpfile.js

var gulp = require('gulp');
var stylus = require('gulp-stylus');
var plumber = require('gulp-plumber');

gulp.task('stylus', function() {
  gulp.src('public/stylesheets/style.styl')
    .pipe(plumber())
    .pipe(stylus())
    .pipe(gulp.dest('public/stylesheets'));
});

gulp.task('watch', function() {
  gulp.watch('public/stylesheets/*.styl', ['stylus']);
});

gulp.task('default', ['stylus', 'watch']);

运行gulp执行

答案 1 :(得分:1)

src: __dirname + '/public'之后似乎缺少逗号。


<强>更新

你应该像这样要求和使用它(首先将它添加到package.json和npm install):

var express        = require('express');
var logger         = require('morgan');
var bodyParser     = require('body-parser');
var app            = express();

app.use(logger('dev'));                     
app.use(bodyParser());