├── 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 += ''+(index+1)+''+product['_id']+''+product['name']+''+product['category']+''+product['price']+''+product['manufacturer']+''; 12 | }); 13 | 14 | $("#trdata").html(string); 15 | }); 16 | }); 17 | 18 | $("#importdata").on('click', function(){ 19 | $.get( "/import", function( data ) { 20 | $("#message").show().html(data['success']); 21 | }); 22 | }); 23 | 24 | }); -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var csv = require("fast-csv"); 3 | var router = express.Router(); 4 | var fs = require('fs'); 5 | 6 | var mongoose = require('mongoose'); 7 | 8 | var Product = mongoose.model('Products'); 9 | 10 | var csvfile = __dirname + "/../public/files/products.csv"; 11 | var stream = fs.createReadStream(csvfile); 12 | 13 | 14 | /* GET home page. */ 15 | router.get('/', function(req, res, next) { 16 | 17 | res.render('index', { title: 'Import CSV using NodeJS' }); 18 | 19 | }).get('/import', function(req, res, next) { 20 | 21 | var products = [] 22 | var csvStream = csv() 23 | .on("data", function(data){ 24 | 25 | var item = new Product({ 26 | name: data[0] , 27 | price: data[1] , 28 | category: data[2], 29 | description: data[3], 30 | manufacturer:data[4] 31 | }); 32 | 33 | item.save(function(error){ 34 | console.log(item); 35 | if(error){ 36 | throw error; 37 | } 38 | }); 39 | 40 | }).on("end", function(){ 41 | 42 | }); 43 | 44 | stream.pipe(csvStream); 45 | res.json({success : "Data imported successfully.", status : 200}); 46 | 47 | }).get('/fetchdata', function(req, res, next) { 48 | 49 | Product.find({}, function(err, docs) { 50 | if (!err){ 51 | res.json({success : "Updated Successfully", status : 200, data: docs}); 52 | } else { 53 | throw err; 54 | } 55 | }); 56 | 57 | }); 58 | module.exports = router; 59 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var favicon = require('serve-favicon'); 4 | var logger = require('morgan'); 5 | var cookieParser = require('cookie-parser'); 6 | var bodyParser = require('body-parser'); 7 | 8 | var session = require('express-session'); 9 | var flash = require('connect-flash'); 10 | 11 | var mongoose = require('mongoose'); 12 | mongoose.Promise = global.Promise; 13 | mongoose.connect('mongodb://localhost/dbproducts', { useMongoClient: true }); 14 | require("./models/Product"); 15 | 16 | var index = require('./routes/index'); 17 | var users = require('./routes/users'); 18 | 19 | var app = express(); 20 | 21 | app.use(session({ cookie: { maxAge: 60000 }, 22 | secret: 'woot', 23 | resave: false, 24 | saveUninitialized: false})); 25 | 26 | // view engine setup 27 | app.set('views', path.join(__dirname, 'views')); 28 | app.set('view engine', 'pug'); 29 | 30 | // uncomment after placing your favicon in /public 31 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 32 | app.use(logger('dev')); 33 | app.use(bodyParser.json()); 34 | app.use(bodyParser.urlencoded({ extended: false })); 35 | app.use(cookieParser()); 36 | app.use(express.static(path.join(__dirname, 'public'))); 37 | 38 | app.use(flash()); 39 | 40 | 41 | app.use('/', index); 42 | app.use('/users', users); 43 | 44 | // catch 404 and forward to error handler 45 | app.use(function(req, res, next) { 46 | var err = new Error('Not Found'); 47 | err.status = 404; 48 | next(err); 49 | }); 50 | 51 | // error handler 52 | app.use(function(err, req, res, next) { 53 | // set locals, only providing error in development 54 | res.locals.message = err.message; 55 | res.locals.error = req.app.get('env') === 'development' ? err : {}; 56 | 57 | // render the error page 58 | res.status(err.status || 500); 59 | res.render('error'); 60 | }); 61 | 62 | module.exports = app; 63 | -------------------------------------------------------------------------------- /bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('import-csv-nodejs:server'); 9 | var http = require('http'); 10 | 11 | /** 12 | * Get port from environment and store in Express. 13 | */ 14 | 15 | var port = normalizePort(process.env.PORT || '3000'); 16 | app.set('port', port); 17 | 18 | /** 19 | * Create HTTP server. 20 | */ 21 | 22 | var server = http.createServer(app); 23 | 24 | /** 25 | * Listen on provided port, on all network interfaces. 26 | */ 27 | 28 | server.listen(port); 29 | server.on('error', onError); 30 | server.on('listening', onListening); 31 | 32 | /** 33 | * Normalize a port into a number, string, or false. 34 | */ 35 | 36 | function normalizePort(val) { 37 | var port = parseInt(val, 10); 38 | 39 | if (isNaN(port)) { 40 | // named pipe 41 | return val; 42 | } 43 | 44 | if (port >= 0) { 45 | // port number 46 | return port; 47 | } 48 | 49 | return false; 50 | } 51 | 52 | /** 53 | * Event listener for HTTP server "error" event. 54 | */ 55 | 56 | function onError(error) { 57 | if (error.syscall !== 'listen') { 58 | throw error; 59 | } 60 | 61 | var bind = typeof port === 'string' 62 | ? 'Pipe ' + port 63 | : 'Port ' + port; 64 | 65 | // handle specific listen errors with friendly messages 66 | switch (error.code) { 67 | case 'EACCES': 68 | console.error(bind + ' requires elevated privileges'); 69 | process.exit(1); 70 | break; 71 | case 'EADDRINUSE': 72 | console.error(bind + ' is already in use'); 73 | process.exit(1); 74 | break; 75 | default: 76 | throw error; 77 | } 78 | } 79 | 80 | /** 81 | * Event listener for HTTP server "listening" event. 82 | */ 83 | 84 | function onListening() { 85 | var addr = server.address(); 86 | var bind = typeof addr === 'string' 87 | ? 'pipe ' + addr 88 | : 'port ' + addr.port; 89 | debug('Listening on ' + bind); 90 | } 91 | --------------------------------------------------------------------------------