├── public
├── files.zip
├── files
│ └── products.csv
├── stylesheets
│ └── style.css
└── javascripts
│ └── app.js
├── views
├── error.pug
├── layout.pug
└── index.pug
├── routes
├── users.js
└── index.js
├── package.json
├── models
└── Product.js
├── README.md
├── app.js
└── bin
└── www
/public/files.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/programmer-blog/import-csv-using-nodejs/HEAD/public/files.zip
--------------------------------------------------------------------------------
/views/error.pug:
--------------------------------------------------------------------------------
1 | extends layout
2 |
3 | block content
4 | h1= message
5 | h2= error.status
6 | pre #{error.stack}
7 |
--------------------------------------------------------------------------------
/public/files/products.csv:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/programmer-blog/import-csv-using-nodejs/HEAD/public/files/products.csv
--------------------------------------------------------------------------------
/public/stylesheets/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 50px;
3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 | }
5 |
6 | a {
7 | color: #00B7FF;
8 | }
9 |
--------------------------------------------------------------------------------
/routes/users.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var router = express.Router();
3 |
4 | /* GET users listing. */
5 | router.get('/', function(req, res, next) {
6 | res.send('respond with a resource');
7 | });
8 |
9 | module.exports = router;
10 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "import-csv-nodejs",
3 | "version": "0.0.0",
4 | "private": true,
5 | "scripts": {
6 | "start": "node ./bin/www"
7 | },
8 | "dependencies": {
9 | "body-parser": "~1.18.2",
10 | "cookie-parser": "~1.4.3",
11 | "debug": "~2.6.9",
12 | "express": "~4.15.5",
13 | "mongoose": "^4.13.6",
14 | "morgan": "~1.9.0",
15 | "pug": "2.0.0-beta11",
16 | "serve-favicon": "~2.4.5"
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/models/Product.js:
--------------------------------------------------------------------------------
1 | var mongoose = require('mongoose');
2 |
3 | var Schema = mongoose.Schema;
4 |
5 | var productSchema = new Schema({
6 |
7 | name: { type: String, Required: 'Product name cannot be left blank.' },
8 |
9 | price: { type: String, Required: 'Product price cannot be left blank.'},
10 |
11 | category: { type: String , Required: 'Product category cannot be left blank'},
12 |
13 | description: { type: String },
14 |
15 | manufacturer: { type: String }
16 | });
17 |
18 | module.exports = mongoose.model('Products', productSchema);
--------------------------------------------------------------------------------
/views/layout.pug:
--------------------------------------------------------------------------------
1 | doctype html
2 | html(lang="en")
3 | head
4 | title Bootstrap Example
5 | meta(charset="utf-8")
6 | meta(name="viewport", content="width=device-width, initial-scale=1")
7 | link(rel="stylesheet", href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css")
8 | script(src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js")
9 | script(src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js")
10 |
11 | body
12 | .container
13 | block content
14 | script(src="/javascripts/app.js")
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Import CSV Using NodeJS
2 |
3 | Programmer Blog: https://programmerblog.net/
4 |
5 | Source code for article on Import CSV file using nodejs and MongoDB
6 |
7 | You can read detailed tutorial on our blog: http://programmerblog.net/import-csv-file-using-nodejs/
8 |
9 |
10 | 1. Create a MongoDB database
11 |
12 | 2. Generate an application, to import CSV file using NodeJS
13 |
14 | 3. Install Mongoose module to connect and process data using mongoose application.
15 |
16 | 4. Install Fast-CSV module to import CSV file using nodejs into MongoDB collection
17 |
18 | 5. Install flash-connect module to display a success flash message
19 |
20 | 6. Ajax GET request to fetch and display data using Nodejs and MongoDB
21 |
22 |
23 | Note: Run MongoDB and on command line type command:
24 |
25 | use dbproducts
26 |
27 |
--------------------------------------------------------------------------------
/views/index.pug:
--------------------------------------------------------------------------------
1 | extends layout
2 |
3 | block content
4 | br
5 | h2 Import CSV file using NodeJS and MongoDB
6 | br
7 | p
8 | a(href='javascript:void(0)' class='btn btn-primary' id='importdata') Import CSV
9 | |
10 | a(href='javascript:void(0)' class='btn btn-primary' id='fetchdata') Fetch Data
11 | br
12 | p#message.alert.alert-success(style='display:none')
13 | br
14 | table.table.table-bordered
15 | thead
16 | tr
17 | th #
18 | th Product ID
19 | th Product Name
20 | th Price
21 | th Category
22 | th Manufacturer
23 | tbody#trdata
24 | tr
25 | td(colspan='3') Click on Import CSV button to import and save data to MongoDB.
Click on Fetch Data button to display data from database
--------------------------------------------------------------------------------
/public/javascripts/app.js:
--------------------------------------------------------------------------------
1 | $(function(){
2 |
3 | $("#fetchdata").on('click', function(){
4 | $.get( "/fetchdata", function( data ) {
5 | var products = data['data'];
6 | $("#trdata").html('');
7 | $("#message").hide();
8 | var string = '';
9 | $.each(products, function(index, product ) {
10 |
11 | string += '