Created
February 22, 2016 08:40
-
-
Save aldidas/40ac099739b92181dcdd to your computer and use it in GitHub Desktop.
express.js route file
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 config = require('./conf'), | |
m = require('./model'); | |
var getPosts = function(req, res) { | |
m.models.post.findAll({ | |
order: 'createdAt DESC' | |
}).then(function(result){ | |
res.send(result); | |
}, function(err){ | |
console.log(err); | |
}); | |
}; | |
var getItem = function(req, res) { | |
var id = req.params.id; | |
m.models.post.findOne({ | |
where: { | |
id: id | |
} | |
}).then(function(result){ | |
res.send(result); | |
}, function(err){ | |
console.log(err); | |
}); | |
} | |
var postEntry = function(req, res) { | |
var title = req.body.title, | |
content = req.body.content; | |
m.models.post.sync() | |
.then(function() { | |
return m.models.post.create({ | |
postTitle: title, | |
postContent: content | |
}); | |
}) | |
.then(function(data) { | |
res.send({ | |
status: 'OK', | |
data: data | |
}); | |
}); | |
}; | |
exports.routes = { | |
getPosts: getPosts, | |
getItem: getItem, | |
postEntry: postEntry | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment