2014-01-19 89 views
1

我使用的是「表達」節點JS的模塊按例子here在節點上使用Express模塊​​JS不工作

當我嘗試和運行服務器,然後打開「本地主機: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) 

我已經加入了文件夾結構,在那裏,現在諧音文件夾包括:

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, 
     } 
    }); 


    }); 
+0

'如果(acceptsHtml(req.headers [ '接受']));:

app = express.createServer()後,添加此{'有一個以分號結尾的聲明,這是不正確的 –

回答

4

你有EJS的看法,但你從來沒有告訴過表達這一點,所以對於如何渲染它們一無所知。在線路

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

我安裝了ejs,將index.html移動到視圖,它會拋出這個[error](http://pastebin.com/Znvb54He) – vamsiampolu

+0

@ user2309862:它可能是尋找'index.ejs'而不是'index.html'。 – icktoofay

+1

@ user2309862如果您未指定擴展名('res.render('index.html')'),則Express將假定擴展名是視圖引擎的名稱('res.render('index');// index.ejs')。 –