2015-09-23 13 views
6

Tôi vừa mới làm theo một hướng dẫn đơn giản về cách xây dựng máy chủ Express (https://codeforgeek.com/2014/06/express-nodejs-tutorial/).Máy chủ tốc hành - Không thể POST/

Tôi đang cố gắng mở rộng mã từ hướng dẫn này để tôi có thể trả lời các yêu cầu bài đăng. Tôi muốn làm điều này bằng cách cập nhật tệp json (điều đó xảy ra để được làm đầy với 'người dùng bình luận', và sau đó rerendering tại '/'

./server.js:

var express = require('express'); 
var app = express(); 

// routing configuration 
require('./router/main')(app); 

// ejs configuration 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'ejs'); 
app.engine('html', require('ejs').renderFile); 

// run the server 
var server = app.listen(8080, function(){ 
    console.log('Express server listening on port 8080'); 
}); 

. /router/main.js (router):

var fs = require('fs'); 
var ejs = require('ejs') 

module.exports = function(app){ 

    app.get('/', function(req, res){ 
    var comments = JSON.parse(fs.readFileSync(__dirname + '/../comments.json')); 
    res.render('index.ejs', comments); 
    }); 

    app.post('/', function(req, res){ 
    console.log('here in post'); 
    var name = req.body.name; 
    var message = req.body.message; 
    var newComment = {"name": name, "message": message}; 
    var comments = JSON.parse(fs.readFileSync(__dirname + '/../comments.json')); 
    comments.push(newComment); 
    fs.writeFileSync(__dirname + '/../comments.json', comments, 'utf8'); 
    //redirect to a 'get' on '/' 
    res.redirect('/'); 
    }); 

    app.get('/about', function(req, res){ 
    res.render('about.html') 
    }); 

} 

./views/index.ejs:

<div> 

    <div> 
    <h1> Joe's Forum </h1> 
    <a href='/about'> (about) </a> 
    </div> 

    <div> 
    <ul> 
    <% comments.forEach(function(comment){ %> 
     <li> 
     <%= comment.name %> : <%= comment.message %> 
     </li> 
    <% }); %> 
    </ul> 
    </div> 

    <h2> Enter a new comment </h2> 

    <form action='/' method="post"> 
    Enter your name: <input type='text' name='name'> <br><br> 
    Enter your message: <input type='textarea' name='message'> <br><br> 
    <input type='submit' value='Submit'> 
    <form> 

</div> 

./comments.json:

{ 
    "comments": [ 
    {"name":"Joe", "message" : "What advantages does Node.js afford the web developer?"}, 
    {"name": "John", "message": "Asynchronous IO helps us to keep our pages responsive even if the server is fetching data"} 
    ] 
} 

Khi tôi cố gắng để gửi bình luận mới từ hình thức của tôi, tất cả tôi thấy là thế này:

"Không thể POST /"

Ai đó có thể giải thích tại sao tôi có thể gặp lỗi này? Cảm ơn

+0

Bạn đang làm một cái gì đó thật không-không. Bạn không bao giờ nên sử dụng api 'Sync' của nodeJS và chắc chắn không phải trong phản hồi từ một máy chủ tốc hành. Cũng không có lỗi kiểm tra bất cứ nơi nào, do đó, máy chủ của bạn sẽ rất dễ bị tai nạn. Bạn có thể hiển thị nội dung của 'comments.json' không? – caasjj

+0

Ok đủ công bằng. Tuy nhiên, tôi chỉ muốn 'làm cho nó hoạt động' như một bài tập trong việc dựa vào Express. Tôi biết nó không phải là mã tuyệt vời. Khi tôi nhận được yêu cầu 'đăng bài', tôi có ý định làm sạch nó (tức là đăng ký các phương pháp hay nhất). Theo yêu cầu, tôi sẽ chỉnh sửa để bao gồm comments.json. –

+0

Hoàn toàn không có nghĩa là chỉ trích - chỉ cần chỉ ra điều gì đó bạn có thể hoặc có thể không nhận thức được. Bây giờ, tôi nghĩ rằng tôi cũng hiểu tại sao mã của bạn không đúng. Bạn đang làm 'comments.push' trên một' Object' - thay vì 'Array'. Hãy để tôi xem nếu tôi có thể hack một câu trả lời rõ ràng cho bạn. – caasjj

Trả lời

3

Thực tế có một vài vấn đề, nhưng chính là bạn không có trình phân tích cú pháp nội dung - mô-đun chuyển đổi luồng nút trong POST thành req.body. Tôi hiện chỉ quen thuộc với bodyParser và có lẽ bạn nên nghiên cứu một chút. Mặc dù nó được thể hiện trong tài liệu Express 4.x, bạn nhận được một thông báo phản đối khi bạn chạy máy chủ.

Vấn đề khác là vấn đề của comments.push. Đó phải là comments.comments.push. Các công trình sau đây:

router.js:

var fs = require('fs'); 
var ejs = require('ejs') 

module.exports = function(app){ 

    app.get('/', function(req, res){ 
    var comments = JSON.parse(fs.readFileSync(__dirname + '/../comments.json')); 
    res.render('index.ejs', comments); 
    }); 

    app.post('/', function(req, res){ 
    console.log('here in post'); 
    console.log(req.body) 
    var name = req.body.name; 
    var message = req.body.message; 
    var newComment = {"name": name, "message": message}; 
    var comments = JSON.parse(fs.readFileSync(__dirname + '/../comments.json')); 
    comments.comments.push(newComment); 
    fs.writeFileSync(__dirname + '/../comments.json', JSON.stringify(comments), 'utf8'); 
    //redirect to a 'get' on '/' 
    res.redirect('/'); 
    }); 

    app.get('/about', function(req, res){ 
    res.render('about.html') 
    }); 

} 

và server.js:

var express = require('express'); 
var bodyParser = require('body-parser'); 
var app = express(); 

app.use(bodyParser.urlencoded()) 

// routing configuration 
require('./router/main')(app); 

// ejs configuration 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'ejs'); 
app.engine('html', require('ejs').renderFile); 

// run the server 
var server = app.listen(8080, function(){ 
    console.log('Express server listening on port 8080'); 
}) 
+0

Cảm ơn câu trả lời của bạn. Một khi tôi nhận được 15 danh tiếng, tôi sẽ upvote nó. –

+0

Đánh giá cao điều đó!Hãy xem xét vấn đề phân tích cú pháp cơ thể và phần mềm trung gian nói chung, vì điều đó sẽ chứng minh rất hữu ích về cách Express hoạt động. – caasjj

Các vấn đề liên quan