首先说明一下,我不是专业程序员,但是我的老板找到我后知道我在10年前上过高中的“网页设计”课程后就让我建一个网站。当时静态网站已经足够,甚至CSS也只是开始展现其潜力,但这不是重点。
我现在正在Visual Studio Code上开发一个Node.js项目,但出现了奇怪的异常。虽然代码能正常运行,但我还是好奇想解决这个问题。以下是我的代码:
app.js
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
multer = require('multer'),
controller = require('./controllers');
//Sets the view engine to jade.
app.set('views', __dirname + '/frontend');
app.set('view engine', 'jade');
//sets up the development enviroment
if (app.get('env') === 'development') {
app.locals.pretty = true;
app.use(express.static(__dirname + '/build/public'))
var build = require(__dirname + '/build.js');
app.use('css/*', function(req, res, next){
build.style();
next();
});
}
//Sets up the public directory to serve static files - This will be depricated quite soon...
//app.use(express.static(__dirname + '/public'));
//Initalizes site-wide local variables
//app.set('title', 'Halvorson Homes');
//Sets up body-parser to be able to read RESTful requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(multer({dest: 'tmp'}).fields());
//Load Controllers
app.use( '/' , controller);
//makes the app listen on port 3000
app.listen(3000, function() {
console.log('Listening on port 3000...');
})
我的文件夹结构基本上如下所示:
- controllers
- index.js
- designs.js
- models
- designs.js
- frontend
- common
- layout.jade
- header.jade
- footer.jade
- client.js
- style.less
- index
- index.jade
- client.js
- style.less
- designs
- index.jade
- client.js
- style.less
- common
- build
- tmp
- srv
- app.js
- build.js
- package.json