Created
April 4, 2015 09:27
-
-
Save peerax/7f59f05802c11b5ef538 to your computer and use it in GitHub Desktop.
nodejs+express+ejs .html
This file contains hidden or 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 lang="en"> | |
<meta charset="UTF-8"> | |
<% include ../partials/head %> | |
<title></title> | |
</head> | |
<body> | |
<%= title.date %> | |
</body> | |
</html> |
This file contains hidden or 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 path = require('path'); | |
var mongoose = require('mongoose'); | |
var app = express(); | |
app.engine('html', require('ejs').renderFile); | |
app.set('view engine', 'html'); | |
mongoose.connect('mongodb://localhost/running'); | |
var Event = mongoose.model('runevents', { name: String }); | |
/* | |
var kitty = new Event({ name: 'Zildjian' }); | |
kitty.save(function (err) { | |
if (err) // ... | |
console.log('meow'); | |
}); | |
*/ | |
app.get('/', function(req, res){ | |
res.send('hello world'); | |
}); | |
app.get('/eee', function(req, res){ | |
Event.find({ 'date': 2007 }, function (err, result) { | |
if (err) return handleError(err); | |
console.log(result[0].name); | |
res.render('index', { title: result[0]}); | |
}) | |
}); | |
// For redirect to 404 | |
app.use(function(req, res) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
res.sendFile(path.join(__dirname+'/views/404-error.html')); | |
}); | |
app.listen(3000); | |
console.log('Server running at http://127.0.0.1:3000/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment