-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (77 loc) · 2.04 KB
/
app.js
File metadata and controls
86 lines (77 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var cons = require('consolidate');
var dust = require('dustjs-helpers');
var pg = require('pg');
var app = express();
var exphbs = require('express-handlebars');
var Sequelize = require('Sequelize');
//db connect string
var connection = 'postgres://postgres:admin@localhost:5432/recipebookdb';
// var Article = connection.define('article',{
// slug: {
// type: Sequelize.STRING,
// primaryKey: true
// },
// title: {
// type: Sequelize.STRING,
// unique: true,
// allowNull: false,
// validate:{
// len: {
// args: [10,150],
// msg: 'Please enter a title with at least 10 chars but no more than 150'
// }
// }
// },
// body: {
// type: Sequelize.TEXT,
// defaultValue: 'Coming soon...'
// }
// },{
// // 关闭时间戳功能
// timestamps: false,
// // 关闭自动把表名复数化,比如你建的表是article,实际建成的可能是articles
// freezeTableName: true
// });
// connection
// .sync({
// force: true,
// logging: console.log
// })
// .then(function() {
// return Article.create({
// title: 'ddhhhhjjkkkhkkkkk',
// slug: 'wib',
// body: 'body content'
// })
// })
// .catch(function(error) {
// console.log(error);
// });
app.engine('handlebars',exphbs({defaultLayout: 'main'}));
app.set('view engine','handlebars');
app.get('/',function (req,res) {
// res.render('home');
pg.connect(connection,function (err,client,done) {
if(err){
return console.error('error fetching client from pool',err);
}
client.query('SELECT * FROM recipes',function (err,result) {
if (err) {
return console.error('error runing query',err);
}
done();
res.render('home', {recipes: result.rows});
console.log(JSON.stringify(result.rows[0]));
});
});
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//Set Public Folder
app.use(express.static(path.join(__dirname,'public')));
app.listen(3100,function() {
console.log('APP RUNNING ON PORT 3100!');
});