Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Added an example #163

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions examples/database/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ADDING Modules
const express = require('express');
const path = require("path");
const bodyparser = require("body-parser");
const app = express();

// Specify port
const port = 80;

// _______MONGOOSE______
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true, useUnifiedTopology: true });

// Define mongoose schema
const reviewsSchema = new mongoose.Schema({
customername: String,
productname: Number,
reviews: String,
});

const Reviews = mongoose.model('Reviews',reviewsSchema);

// EXPRESS SPECIFIC STUFF

app.use('/static', express.static('static')); // For serving static files
app.use(express.urlencoded());


// TEMPLATE ENGINE SPECIFIC STUFF
app.set('view engine', 'jsx'); // set the view engine as jsx
app.set('views', path.join(__dirname,'/views')); // set the views directory
app.engine('jsx', require('express-react-views').createEngine());

// ENDPOINTS
app.get('/', (req,res)=>{
const params = {title: 'Customer Reviews'}
res.status(200).render('index',params);
});


app.post('/', (req,res)=>{
const mydata = new Reviews(req.body);
console.log(mydata);
const pra = {message: 'Thanks for giving your reviews, Your reviews are submitted.'}
mydata.save().then(()=>{
res.render('submitted',pra);
}).catch(()=>{
res.status(400).send(" Sorry there's a problem, Item was not saved to the database");
});
});

//START THE SERVER
app.listen(port,()=>{
console.log(`The application is running on the port ${port}`);
});
2,317 changes: 2,317 additions & 0 deletions examples/database/package-lock.json

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions examples/database/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "nodejs-customer-review-app",
"version": "1.0.0",
"description": "Customer reviews taking and showing web application made using node js,express js and mongo db database",
"main": "app.js",
"scripts": {
"test": "npm start"
},
"repository": {
"type": "git",
"url": "https://github.com/tush-tr/nodejs-customer-review-app.git"
},
"author": "Tushar Rajput",
"license": "ISC",
"bugs": {
"url": "https://github.com/tush-tr/nodejs-customer-review-app/issues"
},
"homepage": "https://github.com/tush-tr/nodejs-customer-review-app#readme",
"dependencies": {
"express": "^4.17.1",
"express-react-views": "^0.11.0",
"mongoose": "^5.11.8",
"react": "^17.0.1",
"react-dom": "^17.0.1"
}
}
48 changes: 48 additions & 0 deletions examples/database/static/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Default */
body{
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
/* Form */
form{
border: 2px solid red;
display: flex;
flex-direction: column;
width: 400px;
justify-content: center;
align-items: center;
padding: 10px 10px;
}
.components{
margin: 10px 10px;
}
input{
background-color: cornsilk;
}
textarea{
background-color: cornsilk;

}
.btn{
padding: 6px 10px;
background-color: black;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}

/* Index */
.firstheading{
text-align: center;
background-color: red;
color: white;
width: 100%;
}
/* Responce */

/* reviews */

/* submitted */
15 changes: 15 additions & 0 deletions examples/database/views/default.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var React = require('react');

function DefaultLayout(props) {
return (
<html>
<head>
<title>{props.title}</title>
<link rel="stylesheet" href="../static/index.css" />
</head>
<body>{props.children}</body>
</html>
);
}

module.exports = DefaultLayout;
22 changes: 22 additions & 0 deletions examples/database/views/form.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var React = require('react');
function Form(props){
return(
<form action="/" method="POST">
<span className="components">
<label for="customername">Enter your name: </label>
<input type="text" name="customername" />
</span>
<span className="components">
<label for="productname">Product Name: </label>
<input type="text" name="productname" />
</span>
<span className="components">
<label for="reviews">Your reviews: </label>
<textarea name="reviews" id="" cols="30" rows="10"></textarea>
</span>
<button className="btn">Submit</button>
</form>

)
}
module.exports = Form;
14 changes: 14 additions & 0 deletions examples/database/views/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var React = require('react');
// import './public/css/index.css'
var DefaultLayout = require('./default');
var Form = require('./form');
function App(props) {
return (
<DefaultLayout title={props.title} >
<h1 className="firstheading">Customer Reviews</h1>
<Form />
</DefaultLayout>
);
};

module.exports = App;
9 changes: 9 additions & 0 deletions examples/database/views/responce.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var React = require('react');
const Submit = (props)=>{
return (
<div>
<h2>{props.message}</h2>
</div>
)
}
module.exports = Submit;
Empty file.
13 changes: 13 additions & 0 deletions examples/database/views/submitted.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var React = require('react');
var DefaultLayout = require('./default');
var Responce = require('./responce');
function App(props) {
return (
<DefaultLayout title={props.title} >
<h1>Customer Reviews</h1>
<Responce message={props.message} />
</DefaultLayout>
);
};

module.exports = App;