├── public ├── .gitignore └── index.html ├── mongod ├── package.json ├── README.md └── app.js /public/.gitignore: -------------------------------------------------------------------------------- 1 | data/ 2 | 3 | 4 | -------------------------------------------------------------------------------- /mongod: -------------------------------------------------------------------------------- 1 | mongod --bind_ip=$IP --dbpath=data --nojournal --rest "$@" 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "form-sample", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "body-parser": "^1.17.1", 14 | "express": "^4.15.2", 15 | "mongodb": "^2.2.26" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Save form data to MongoDB 2 | 3 | In this tutorial we will create simple form that will save user data to server implemented in Node.js with MongoDB database and allow us to see saved data on a separate page in JSON format. 4 | 5 | Link to tutorial: [http://programmingmentor.com/post/save-form-nodejs-mongodb/](http://programmingmentor.com/post/save-form-nodejs-mongodb/) 6 | 7 | Link to video: [https://youtu.be/epo-70M8hNo](https://youtu.be/epo-70M8hNo) 8 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Client Data 6 | 7 | 8 |

Please fill data in the form below:

9 |
10 | 11 |
12 | 13 |
14 | 15 |
16 | 17 |
18 | View feedbacks 19 | 20 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var bodyParser = require('body-parser'); 4 | var mongodb = require('mongodb'); 5 | 6 | var dbConn = mongodb.MongoClient.connect('mongodb://localhost:27017'); 7 | 8 | var app = express(); 9 | 10 | app.use(bodyParser.urlencoded({ extended: false })); 11 | app.use(express.static(path.resolve(__dirname, 'public'))); 12 | 13 | app.post('/post-feedback', function (req, res) { 14 | dbConn.then(function(db) { 15 | delete req.body._id; // for safety reasons 16 | db.collection('feedbacks').insertOne(req.body); 17 | }); 18 | res.send('Data received:\n' + JSON.stringify(req.body)); 19 | }); 20 | 21 | app.get('/view-feedbacks', function(req, res) { 22 | dbConn.then(function(db) { 23 | db.collection('feedbacks').find({}).toArray().then(function(feedbacks) { 24 | res.status(200).json(feedbacks); 25 | }); 26 | }); 27 | }); 28 | 29 | app.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0' ); 30 | --------------------------------------------------------------------------------