Created
November 6, 2014 13:07
-
-
Save tommy351/437d5e86096289139724 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>留言板</title> | |
</head> | |
<body> | |
<% messages.forEach(function(line, i){ %> | |
<p> | |
<strong><%= line.author %></strong>: <%= line.content %> | |
<a href="/delete/<%= i %>">刪除</a> | |
<a href="/like/<%= i %>">Like (<%= line.likes %>)</a> | |
</p> | |
<% }) %> | |
<form action="/" method="post"> | |
<input type="text" name="author" placeholder="作者"> | |
<input type="text" name="content" placeholder="內容"> | |
<button type="submit">送出</button> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var fs = require('fs'); | |
var app = express(); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'html'); | |
app.engine('html', require('ejs').renderFile); | |
app.use(require('body-parser').urlencoded({ | |
extended: true | |
})); | |
app.get('/', function(req, res, next){ | |
fs.readFile('database.txt', 'utf8', function(err, content){ | |
var lines = content ? content.split('\n') : []; | |
lines = lines.map(function(item){ | |
var line = item.split(':'); | |
return {author: line[0], content: line[1], likes: line[2]}; | |
}); | |
lines.pop(); | |
res.render('index', {messages: lines}); | |
}); | |
}); | |
app.post('/', function(req, res, next){ | |
var content = req.body.content; | |
var author = req.body.author; | |
fs.appendFile('database.txt', author + ':' + content + ':0\n', function(err){ | |
if (err) return next(err); | |
res.redirect('/'); | |
}); | |
}); | |
app.get('/delete/:id', function(req, res, next){ | |
var line = req.params.id; | |
fs.readFile('database.txt', 'utf8', function(err, content){ | |
if (!content) return res.redirect('/'); | |
var lines = content.split('\n'); | |
lines.splice(line, 1); | |
fs.writeFile('database.txt', lines.join('\n'), function(err){ | |
if (err) return next(err); | |
res.redirect('/'); | |
}); | |
}); | |
}); | |
app.get('/like/:id', function(req, res, next){ | |
var line = req.params.id; | |
fs.readFile('database.txt', 'utf8', function(err, content){ | |
if (!content) return res.redirect('/'); | |
var lines = content.split('\n'); | |
var item = lines[line].split(':'); | |
var likes = parseInt(item[2]) || 0; | |
item[2] = likes + 1; | |
lines[line] = item.join(':'); | |
fs.writeFile('database.txt', lines.join('\n'), function(err){ | |
if (err) return next(err); | |
res.redirect('/'); | |
}) | |
}); | |
}); | |
app.listen(4000, function(){ | |
console.log('Server started'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment