Initial commit
This commit is contained in:
commit
3b2ce3e4bf
55
app.js
Normal file
55
app.js
Normal file
@ -0,0 +1,55 @@
|
||||
var createError = require('http-errors');
|
||||
var express = require('express');
|
||||
var path = require('path');
|
||||
var cookieParser = require('cookie-parser');
|
||||
var logger = require('morgan');
|
||||
const Handlebars = require('handlebars')
|
||||
const hbs = require("express-handlebars")
|
||||
const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access')
|
||||
|
||||
//var ExpressValidator = require("express-validator")
|
||||
//var ExpressSession = require("express-session")
|
||||
|
||||
var index_router = require('./routes/index');
|
||||
|
||||
var app = express();
|
||||
|
||||
// view engine setup
|
||||
app.engine("hbs", hbs({
|
||||
extname: "hbs",
|
||||
defaultLayout: "layout",
|
||||
layoutsDir: __dirname + "/views/layouts/",
|
||||
handlebars: allowInsecurePrototypeAccess(Handlebars)
|
||||
}))
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'hbs');
|
||||
|
||||
app.use(logger('dev'));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: false }));
|
||||
app.use(cookieParser());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
//app.use(ExpressSession({secret: "max", saveUninitialized: false, resave: false}))
|
||||
|
||||
app.use('/', index_router);
|
||||
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use((req, res, next) => {
|
||||
res.statusCode = 404
|
||||
res.statusMessage = "Not Found"
|
||||
next();
|
||||
});
|
||||
|
||||
// error handler
|
||||
app.use((err, req, res, next) => {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message;
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500);
|
||||
res.render('error');
|
||||
});
|
||||
|
||||
module.exports = app;
|
78
bin/www.js
Executable file
78
bin/www.js
Executable file
@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var app = require('../app');
|
||||
var debug = require('debug')('tic-tac-toe:server');
|
||||
var http = require('http');
|
||||
|
||||
/* Get port from environment and store in Express. */
|
||||
|
||||
var port = normalizePort(process.env.PORT || '8000');
|
||||
app.set('port', port);
|
||||
|
||||
/* Create HTTP server. */
|
||||
|
||||
var server = http.createServer(app);
|
||||
|
||||
/* Listen on provided port, on all network interfaces. */
|
||||
|
||||
server.listen(port);
|
||||
server.on('error', onError);
|
||||
server.on('listening', onListening);
|
||||
|
||||
/* Normalize a port into a number, string, or false. */
|
||||
|
||||
function normalizePort(val) {
|
||||
var port = parseInt(val, 10);
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
return val;
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
// port number
|
||||
return port;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Event listener for HTTP server "error" event. */
|
||||
|
||||
function onError(error) {
|
||||
if (error.syscall !== 'listen') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
var bind = typeof port === 'string'
|
||||
? 'Pipe ' + port
|
||||
: 'Port ' + port;
|
||||
|
||||
// handle specific listen errors with friendly messages
|
||||
switch (error.code) {
|
||||
case 'EACCES':
|
||||
console.error(bind + ' requires elevated privileges');
|
||||
process.exit(1);
|
||||
break;
|
||||
case 'EADDRINUSE':
|
||||
console.error(bind + ' is already in use');
|
||||
process.exit(1);
|
||||
break;
|
||||
default:
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/* Event listener for HTTP server "listening" event. */
|
||||
|
||||
function onListening() {
|
||||
var addr = server.address();
|
||||
var bind = typeof addr === 'string'
|
||||
? 'pipe ' + addr
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
}
|
1723
package-lock.json
generated
Normal file
1723
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
24
package.json
Normal file
24
package.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "mongodb-example",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ./bin/www.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@handlebars/allow-prototype-access": "^1.0.3",
|
||||
"cookie-parser": "~1.4.4",
|
||||
"debug": "~2.6.9",
|
||||
"express": "~4.16.1",
|
||||
"express-handlebars": "^5.1.0",
|
||||
"express-session": "^1.17.1",
|
||||
"express-validator": "^6.6.1",
|
||||
"http-errors": "~1.6.3",
|
||||
"mongodb": "^3.6.2",
|
||||
"mongoose": "^5.10.4",
|
||||
"morgan": "~1.9.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.4"
|
||||
}
|
||||
}
|
78
public/stylesheets/style.css
Normal file
78
public/stylesheets/style.css
Normal file
@ -0,0 +1,78 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #00B7FF;
|
||||
}
|
||||
|
||||
section {
|
||||
float: left;
|
||||
background: #bdc3c7;
|
||||
padding: 10px;
|
||||
margin: 30px;
|
||||
width: 300px;
|
||||
box-shadow: 3px 3px 1px #34495e;
|
||||
min-height: 400px;
|
||||
}
|
||||
|
||||
section:first-of-type {
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
section:last-of-type {
|
||||
margin-right: 0;
|
||||
}
|
||||
|
||||
section h3 {
|
||||
border-bottom: 1px solid black;
|
||||
padding-bottom: 5px;
|
||||
}
|
||||
|
||||
.insert {
|
||||
background: #2ecc71;
|
||||
}
|
||||
|
||||
.get {
|
||||
background: #ecf0f1;
|
||||
}
|
||||
|
||||
.update {
|
||||
background: #3498db;
|
||||
}
|
||||
|
||||
.delete {
|
||||
background: #e74c3c;
|
||||
}
|
||||
|
||||
.input label {
|
||||
display: block;
|
||||
font-weight: bold;
|
||||
padding: 2px 0;
|
||||
}
|
||||
|
||||
input,
|
||||
button {
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
button {
|
||||
margin-top: 10px;
|
||||
border: none;
|
||||
box-shadow: 1px 1px 1px #34495e;
|
||||
border-radius: 0;
|
||||
background: #ecf0f1;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #bdc3c7;
|
||||
}
|
||||
|
||||
.item {
|
||||
margin: 10px 0;
|
||||
padding: 5px;
|
||||
background: #95a5a6;
|
||||
border: 1px solid black;
|
||||
}
|
60
routes/index.js
Normal file
60
routes/index.js
Normal file
@ -0,0 +1,60 @@
|
||||
const { Router } = require('express');
|
||||
const mongoose = require("mongoose")
|
||||
const { MongoClient, ObjectID } = require("mongodb")
|
||||
const assert = require("assert");
|
||||
const { error } = require('console');
|
||||
var router = Router();
|
||||
mongoose.connect("mongodb://localhost:27017/test", { useUnifiedTopology: true })
|
||||
var Schema = mongoose.Schema
|
||||
|
||||
const userDataSchema = new Schema({
|
||||
title: {type: String, required: true},
|
||||
content: String,
|
||||
author: String
|
||||
}, {collation: "user_data"})
|
||||
|
||||
const UserData = mongoose.model("UserData", userDataSchema)
|
||||
|
||||
router.get('/', (req, res, next) => {
|
||||
res.render('index');
|
||||
});
|
||||
|
||||
router.get("/get-data", (req, res, next) => {
|
||||
UserData.find()
|
||||
.then(doc => {
|
||||
console.log(doc)
|
||||
res.render("index", {items: doc})
|
||||
})
|
||||
})
|
||||
|
||||
router.post("/insert", (req, res, next) => {
|
||||
let data = new UserData({
|
||||
title: req.body.title,
|
||||
content: req.body.content,
|
||||
author: req.body.author
|
||||
})
|
||||
data.save()
|
||||
|
||||
res.redirect("/")
|
||||
})
|
||||
|
||||
router.post("/update", (req, res, next) => {
|
||||
let id = req.body.id
|
||||
UserData.findById(id, (err, doc) => {
|
||||
if (err) {
|
||||
console.error("No entry found!")
|
||||
}
|
||||
doc.title = req.body.title
|
||||
doc.content = req.body.content
|
||||
doc.author = req.body.author
|
||||
})
|
||||
res.redirect("/")
|
||||
})
|
||||
|
||||
|
||||
router.post("/delete", (req, res, next) => {
|
||||
let id = req.body.id
|
||||
UserData.findByIdAndRemove(id).exec()
|
||||
res.redirect("/")
|
||||
})
|
||||
module.exports = router;
|
3
views/error.hbs
Normal file
3
views/error.hbs
Normal file
@ -0,0 +1,3 @@
|
||||
<h1>{{ message }}</h1>
|
||||
<h2>{{ error.status }}</h2>
|
||||
<pre>{{ error.stack }}</pre>
|
66
views/index.hbs
Normal file
66
views/index.hbs
Normal file
@ -0,0 +1,66 @@
|
||||
<h1>MONGODB - EXERCISE</h1>
|
||||
<section class="insert">
|
||||
<h3>Insert Data</h3>
|
||||
<form action="/insert" method="post">
|
||||
<div class="input">
|
||||
<label for="title">Title</label>
|
||||
<input type="text" id="title" name="title">
|
||||
</div>
|
||||
<div class="input">
|
||||
<label for="content">Content</label>
|
||||
<input type="text" id="content" name="content">
|
||||
</div>
|
||||
<div class="input">
|
||||
<label for="author">Author</label>
|
||||
<input type="text" id="author" name="author">
|
||||
</div>
|
||||
<button type="submit">INSERT</button>
|
||||
</form>
|
||||
</section>
|
||||
<section class="get">
|
||||
<h3>Get Data</h3>
|
||||
<a href="/get-data">LOAD DATA</a>
|
||||
<div>
|
||||
{{# each items }}
|
||||
<article class="item">
|
||||
<div>Title: {{ this.title }}</div>
|
||||
<div>Content: {{ this.content }}</div>
|
||||
<div>Author: {{ this.author }}</div>
|
||||
<div>ID: {{ this._id }}</div>
|
||||
</article>
|
||||
{{/each}}
|
||||
</div>
|
||||
</section>
|
||||
<section class="update">
|
||||
<h3>Update Data</h3>
|
||||
<form action="/update" method="post">
|
||||
<div class="input">
|
||||
<label for="id">ID</label>
|
||||
<input type="text" id="id" name="id">
|
||||
</div>
|
||||
<div class="input">
|
||||
<label for="title">Title</label>
|
||||
<input type="text" id="title" name="title">
|
||||
</div>
|
||||
<div class="input">
|
||||
<label for="content">Content</label>
|
||||
<input type="text" id="content" name="content">
|
||||
</div>
|
||||
<div class="input">
|
||||
<label for="author">Author</label>
|
||||
<input type="text" id="author" name="author">
|
||||
</div>
|
||||
<button type="submit">UPDATE</button>
|
||||
</form>
|
||||
</section>
|
||||
</section>
|
||||
<section class="delete">
|
||||
<h3>Delete Data</h3>
|
||||
<form action="/delete" method="post">
|
||||
<div class="input">
|
||||
<label for="id">ID</label>
|
||||
<input type="text" id="id" name="id">
|
||||
</div>
|
||||
<button type="submit">DELETE</button>
|
||||
</form>
|
||||
</section>
|
11
views/layouts/layout.hbs
Normal file
11
views/layouts/layout.hbs
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet", href="/stylesheets/style.css">
|
||||
<title>{{ title }}</title>
|
||||
</head>
|
||||
<body>
|
||||
{{{ body }}}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user