express实现todolist
Posted bbcfive
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了express实现todolist相关的知识,希望对你有一定的参考价值。
app.js
var express = require(‘express‘); var todoController = require(‘./controllers/todoController.js‘); var app = express(); app.set(‘view engine‘, ‘ejs‘); app.use(express.static(‘./assets‘)); todoController(app); app.listen(3000); console.log(‘listen to port 3000‘);
todo.ejs
<form> <input type="text" name="item" placeholder="Add new item..." required/> <button type="submit">Add Item</button> </form> <ul> <% todos.forEach(function(todoItem) { %> <li><%= todoItem.item %></li> <% }) %> </ul> </div>
todoController.js
var bodyParser = require(‘body-parser‘); var urlencodedParser = bodyParser.urlencoded({ extended: false}); var mongoose = require(‘mongoose‘); mongoose.connect(‘mongodb://bbcfive:[email protected]:37047/todolistdatabase‘); var todoSchema = new mongoose.Schema({ item: String }); var Todo = mongoose.model(‘Todo‘, todoSchema); /* var todoOne = Todo({item: ‘buy flowers‘}).save(function(err){ if (err) throw err; console.log(‘saved‘); }); var data = [ {item: ‘get milk‘}, {item: ‘walk dog‘}, {item: ‘kick some coding ass‘} ]; */ module.exports = function(app) { app.get(‘/todo‘, function(req, res) { Todo.find({}, function(err, data) { if (err) throw err; res.render(‘todo‘, { todos : data}); }); }); app.post(‘/todo‘, urlencodedParser, function(req, res) { var todoOne = Todo(req.body).save(function(err, data){ if (err) throw err; res.json(data); }); /* data.push(req.body); res.json(data); */ }); app.delete(‘/todo/:item‘, function(req, res) { /* data = data.filter(function (todoItem) { return todoItem.item.replace(/ /g, ‘-‘) != req.params.item; }); */ Todo.find({item: req.params.item.replace(/-/g, ‘ ‘)}).remove(function(err, data){ if (err) throw err; res.json(data); }); }); }
以上是关于express实现todolist的主要内容,如果未能解决你的问题,请参考以下文章
[js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist