0

我正在使用 express 和 nunjucks 模板引擎来构建节点应用程序。

模板引擎.js

var nunjucks  = require('nunjucks');

module.exports = function (app) {
    // store environment env
    var env = nunjucks.configure(['views/www'], {
        autoescape: true,
        express   : app
    });
}

应用程序.js

var express=require('express'),
    app=express(),
    engines = require('consolidate'),
    nunjucks  = require('nunjucks');
    MongoClient = require('mongodb').MongoClient,
    http = require("http"),
    assert = require('assert');
    require('./templateEngine.js')(app);

app.set('view engine', 'html');

MongoClient.connect("mongodb://localhost:27017/firstDb",function(err,db){
  assert.equal(null,err);
    console.log("Sucessfully Connected to the Server");
  app.get('/',function(req,res){
    var query = {name : 'nobody'};
    db.collection('friends').find().toArray(function(err,docs){
      console.log(docs)
      res.render('index',{'friendNames':docs})
    })
  })
})

var server = app.listen(3000,function(){
  var port=server.address().port;
  console.log('Express server listening on port', port);
})

当我尝试加载

<link href="css/style.css" rel="stylesheet">

在 index.html 中它说无法获取“ http://localhost:3000/css/style.css

我的文件夹结构是

Root
  views
    www
     css
       style.css
     index.html
  app.js
  templateEngine.js

如果有人早些时候遇到此错误,请帮助我。提前致谢!

4

0 回答 0