1.引入模块node-sass-middleware;
npm install node-sass-middleware
2.在app.js中配置使用
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var sassMiddleware = require('node-sass-middleware');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('html',require("ejs").__express);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
//配置sass
app.use(sassMiddleware({
/* Options */
src: __dirname+"/public",
dest: path.join(__dirname, 'public'),
debug: true,
outputStyle: 'compressed'
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
/*暂时不知道为什么src只能这样写,才能正常编译(就是只能编译'public/stylesheet'文件下的.scss文件)
*src官方解释是定义要编译文件的目录,但是只能这样定义才能正常编译,有知道的大佬还请留言共同学习一下。
*/