Professional Documents
Culture Documents
1 de 22
http://zellwk.com/blog/crud-express-mongodb/
26/08/2016 13:58
2 de 22
http://zellwk.com/blog/crud-express-mongodb/
26/08/2016 13:58
3 de 22
http://zellwk.com/blog/crud-express-mongodb/
$ node -v
npm init
package.json
$ npm init
node
server.js
26/08/2016 13:58
4 de 22
http://zellwk.com/blog/crud-express-mongodb/
$ touch server.js
server.js
console.log
server.js
node server.js
package.json
26/08/2016 13:58
5 de 22
http://zellwk.com/blog/crud-express-mongodb/
server.js
listen
app.listen(3000, function() {
})
console.log('listening on 3000')
node server.js
localhost:3000
get
app.get(path, callback)
26/08/2016 13:58
6 de 22
http://zellwk.com/blog/crud-express-mongodb/
path
localhost:3000
localhost:3000/
// do something here
send
res.send('Hello World')
// Note: request and response are usually written as req and res respectively.
function()
res.send('hello world')
CTRL + C
node server.js
localhost:3000
26/08/2016 13:58
7 de 22
http://zellwk.com/blog/crud-express-mongodb/
index.html
sendFile
res
res.sendFile(__dirname + '/index.html')
// Note: __dirname is the path to your current working directory. Try logging it
sendFile
index.html
touch index.html
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MY APP</title>
</head>
<body>
</body>
</html>
26/08/2016 13:58
8 de 22
http://zellwk.com/blog/crud-express-mongodb/
server.js
--save-dev
devDependency
package.json
nodemon server.js
-g
node_modules
$ ./node_modules/.bin/nodemon server.js
26/08/2016 13:58
9 de 22
http://zellwk.com/blog/crud-express-mongodb/
script
package.json
// ...
"scripts": {
}
// ...
nodemon server.js
<form>
<form>
<form>
index.html
action
method
name
<input
</form>
26/08/2016 13:58
10 de 22
action
http://zellwk.com/blog/crud-express-mongodb/
/quotes
method
post
console.log('Hellooooooooooooooooo!')
Hellooooooooooooooooo!
<form>
use
26/08/2016 13:58
11 de 22
http://zellwk.com/blog/crud-express-mongodb/
app.use(bodyParser.urlencoded({extended: true}))
// All your handlers here...
urlencoded
<form>
req.body
body
request
console.log
console.log(req.body)
26/08/2016 13:58
12 de 22
http://zellwk.com/blog/crud-express-mongodb/
Mongo.Client
sandbox
26/08/2016 13:58
13 de 22
http://zellwk.com/blog/crud-express-mongodb/
MongoClient.connect
26/08/2016 13:58
14 de 22
http://zellwk.com/blog/crud-express-mongodb/
app.listen
db
connect
var db
MongoClient.connect('your-mongodb-url', (err, database) => {
if (err) return console.log(err)
db = database
app.listen(3000, () => {
})
})
console.log('listening on 3000')
quotes
quotes
quotes
db.collection()
save
26/08/2016 13:58
15 de 22
http://zellwk.com/blog/crud-express-mongodb/
console.log('saved to database')
})
})
res.redirect('/')
<form>
collection
find
26/08/2016 13:58
16 de 22
http://zellwk.com/blog/crud-express-mongodb/
})
find
cursor
console.log
cursor
toArray
toArray
console.log()
db.collection('quotes').find().toArray(function(err, results) {
console.log(results)
})
26/08/2016 13:58
17 de 22
http://zellwk.com/blog/crud-express-mongodb/
index.html
ejs
view engine
ejs
view engine
response
render
render
res.render(view, locals)
views
views
index.ejs
views
26/08/2016 13:58
18 de 22
http://zellwk.com/blog/crud-express-mongodb/
mkdir views
touch views/index.ejs
index.ejs
<ul class="quotes">
</li>
<% } %>
</ul>
<%=
<%
%>
%>
quotes[i].name
<form>
index.ejs
quotes[i].quote
index.html
quotes
index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MY APP</title>
</head>
<body>
<ul class="quotes">
26/08/2016 13:58
19 de 22
http://zellwk.com/blog/crud-express-mongodb/
</li>
<% } %>
</ul>
</form>
</body>
</html>
index.ejs
index.ejs
quotes
})
})
26/08/2016 13:58
20 de 22
http://zellwk.com/blog/crud-express-mongodb/
26/08/2016 13:58
21 de 22
53 Comments
http://zellwk.com/blog/crud-express-mongodb/
Zell's Blog
Recommend 14
Login
Sort by Newest
Share
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'ejs');
var db;
db = database
app.listen(3000, () => {
console.log('listening on 3000')
})
})
Reply Share
see more
Thanks a lot ! I wish all the documentations / walkthroughs should be crystal clear
like this.
Reply Share
Super helpful! This helped fill in a lot of gaps in my mind about how this all fits
together! Still have some ways to go though ;)
Reply Share
How would you connect to mongodb using mongoose and a local instance?
Can I use the same MongoClient function and just add my mongoose.connect
instead?
***UPDATE*** if anyone is trying to use a local instance of MongoDB with
mongoose, you can connect using:
26/08/2016 13:58
22 de 22
http://zellwk.com/blog/crud-express-mongodb/
26/08/2016 13:58