javascript - 在 Node js 中使用 Express 模块不起作用

标签 javascript node.js

我正在按照示例使用 Node JS 的“快速”模块 here

当我尝试运行服务器并打开“localhost:8000”时,出现错误:

Error: No default engine was specified and no extension was provided.
  at new View (/home/anr/node_modules/express/lib/view.js:41:42)
  at Function.app.render (/home/anr/node_modules/express/lib/application.js:486:12)
  at ServerResponse.res.render (/home/anr/node_modules/express/lib/response.js:798:7)
  at app.post.res.send.status (/home/anr/Desktop/node js/mvc/ntwitter.js:16:7)
  at callbacks (/home/anr/node_modules/express/lib/router/index.js:164:37)
  at param (/home/anr/node_modules/express/lib/router/index.js:138:11)
  at pass (/home/anr/node_modules/express/lib/router/index.js:145:5)
  at Router._dispatch (/home/anr/node_modules/express/lib/router/index.js:173:5)
  at Object.router (/home/anr/node_modules/express/lib/router/index.js:33:10)
  at next (/home/anr/node_modules/express/node_modules/connect/lib/proto.js:193:15)

我在那里添加了文件夹结构,现在 partials 文件夹包括:

chirps.html  index.html  styles.html  view0.html

view0 由基本的 html 模板组成:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <!-- There is a call to partial here-->
    <%- partial('partials/stylesheet', stylesheets) %>
    <title><%= title %></title>
  </head>
  <body>
    <h1><%= header %></h1>
    <%- body %>
  </body>
</html>

我的索引部分代码在这里:

<form method="POST"  action="/send">
  <input type="text" length="140" name="tweet"/>
  <input type="submit" value="Tweet"/>
</form>
<%- partial('partials/chirp','tweets') %>

这是我的唧唧声部分:

<p><%= chirps %></p>

这是我的 app.js:

var express=require('express');

//express gets http behind the scenes...
var app = express.createServer();
app.listen(8000);
var tweets=[];

//use http verbs instead of registering event listeners to http
//responds only to a get request...

app.get('/',function(req,res){

  //a conveniance method that does request headers,sends the message and ends the response
  //res.send('Welcome to Node Twitter');
  var title='Chirpie',
  header='Welcome To Chirpie';
  res.render('index', {
    locals:{
      'title':title,
      'header':header,
      'tweets':tweets,
      //'stylesheets':['public/style.css']
    }
  });
});

//the second argument in the post request is known as middleware
app.post('/send',express.bodyParser(),function(req,res){
  if(req.body && req.body.tweet) {
    tweets.push(req.body.tweet);
    if (acceptsHtml(req.headers['accept'])); {
      console.log('tweet recieved');
      res.redirect('/', 302); // the 302 status code indicates that this is not a permanent move
      //go to /send before redirecting every time
    }
  } else {  
    console.log('tweet recieved');
    res.send({ status: 'ok', message: 'tweet recieved' });
  } 
});

app.get('/tweets',function(req,res) {
  res.send(tweets);
});

function acceptsHtml(header) {
  var accepts=header.split(',');
  for(var i=0;i<accepts.length;i+=1) {
    if(accepts[i]==='text/html') return true;
  }
  return false;
}

编辑:

通过评论修复的一些问题:

1. Fixed `if (acceptsHtml(req.headers['accept']));`.Removed the semicolon there.
2.The files have to use `.ejs` unless the render specifies it as `.html` which requires the html module.Also move the `index.ejs` to `views`.
3.The reference to `chirp.ejs` was removed.Another point to note would be that the rendering engine is supposed to handle these calls now, the function `partial` no longer. exists,if you wish to use something like that use `include` instead.

所以在所有这一切结束时,这条路线的响应代码是:

app.get('/',function(req,res){
//a conveniance method that does request headers,sends the message and ends the response
//res.send('Welcome to Node Twitter');
var title='Chirpie',
    header='Welcome To Chirpie';

    res.render('index',
        {
        locals:{
        'title':title,
        'header':header,
        'tweets':tweets,
        }
    });


   });

最佳答案

你有 EJS View ,但你从未告诉 Express,所以它对如何呈现它们一无所知。

app = express.createServer() 之后,添加:

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

关于javascript - 在 Node js 中使用 Express 模块不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213342/

相关文章:

javascript - 将任何十进制数字向上取整的最快方法

javascript - 陷入 karma 报应验证作业(nodejs,angularjs)

Node.js sinon 在并行执行中 stub 函数导致测试失败

javascript - js ':' 预期 Node js

Android:使用json上传图片

javascript - 如何在示例中构建像这样的简单图像旋转效果?

javascript - Famo-us Surface 内的 Google map 无法在移动设备上拖动

javascript - 没有 `*` 的结构指令?

javascript - 在 Angular 1.3 中将对象注入(inject)到嵌入内容的范围中

node.js - 通过多个页面将变量保存在socket.io中