使用node.js渲染表达模板的问题

时间:2015-07-30 06:50:06

标签: node.js http express

我在运行node.js服务器时遇到了渲染'EJS'Express模板的问题。我提供了以下代码。如您所见,我正在实现http.createServer(function (req, res)来向客户端读取和写入消息。但是,不会呈现包含HTML代码的模板。代码的主要目标是让客户端将消息“Hello from Client”发布到服务器,服务器使用“Hello from Server”响应客户端。

app.js(服务器端)

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path')
  , request = require ('request')
  , cheerio = require ('cheerio')
  , $;
 

var app = express();
//console.log($('[class = "orange"]').attr('id'));
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(function (req, res) {
var body="";
    console.log('Request received: ');
    req.on('data', function (chunk) {
        body +=chunk;        
    });
    req.on('end', function(){
    	console.log("Body: "+body);
    	res.write('Hello from Server');
    	res.end();
    });

}).listen(3000);

index.ejs(客户端)

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>

  <body>
    <h1><%= title %></h1>
      <input type="button" id="stopButton" value="Button1"/>
      <input type="button" id="stopButton2" value="Button2"/>
    <p>Welcome to <%= title %></p>
  <ul id="fruits">
  <li id= "1" class="apple">Apple</li>
  <li id = "2" class="orange">Orange</li>
  <li id = "3" class="pear">Pear</li>
</ul>
  <script type="text/javascript">
      $(document).ready(function () {
          var formData = {data: "Hello from Client"};
           $.ajax({
               url: 'http://localhost:3000',
               dataType: "json",
               type: 'POST',
               data: formData,
               jsonpCallback: 'callback', 
               success: function (data, textStatus, jqXHR) {
                var ret = jQuery.parseJSON(data);
                console.log(ret.msg);
               },
               error: function (jqXHR, textStatus, errorThrown) {
                 console.log("Error received");
               }
          });
      });
 </script> 
 </body>
</html>

的package.json

{
  "name": "NewProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.2.6",
    "ejs": "*"
  }
}

之前我曾尝试使用有效的app.get

使用app.get()request()http.createServer()之间的主要区别是什么?我们什么时候应该用什么?

1 个答案:

答案 0 :(得分:0)

它不会那样工作。在文档上有一个厕所:

例如:

<XPath>/patient:feed/patient:link[@rel='next']/@href</XPath>

另外,请使用最新版本的Express,你的已经过时了。