Node.js: Convert CSV to JSON and save it in a variable - javascript

So I'm having this code as a base and I have to use it to convert the content of "world_data.csv" to JSON.
I don't have a clue about how I can save that JSON in a variable. I guess the data I want is stored temporarly in "jsonArray", but how can I define a global variable which stores that data indefinetely?
var express = require('express');
var app = express();
var sys = require('util');
var path = require('path');
var bodyParser = require('body-parser');
var Converter = require("csvtojson").Converter;
app.use( bodyParser.json() );
app.use( express.static( path.join(__dirname, "public") ) );
var converter = new Converter({});
converter.on("end_parsed", function (jsonArray) {
console.log(jsonArray);
});
require("fs").createReadStream("world_data.csv").pipe(converter);
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});

You need simply a global var. Plese read Javascript Closures
var express = require('express');
var app = express();
var sys = require('util');
var path = require('path');
var bodyParser = require('body-parser');
var Converter = require("csvtojson").Converter;
var jsonContent = {}; // <-- Your Global JSON Data
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));
var converter = new Converter({});
converter.on("end_parsed", function(jsonArray) {
if (jsonArray) {
jsonContent = jsonArray
}
});
require("fs").createReadStream("world_data.csv").pipe(converter);
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});

Depends what you want with parsed result, for e.g pass it to the view:
app.get('/', function(req, res) {
require("fs").createReadStream("./world_data.csv").pipe(converter);
converter.on("end_parsed", function (json) {
res.json(json)
});
});
As csvtojson provides api and hooks you can call whatever you need on parsing process finish.
Or you can use oneliner that can be implemented as:
app.get('/', function(req, res) {
require("fs").createReadStream("./world_data.csv").pipe(new Converter({constructResult:false})).pipe(res)
})

Related

socket.io application keeps loading

Why does my socket.io webapplication keeps loading, i've implemented auth with certifications, but when i try to access localhost, it keeps loading. ive tried follow this doc, but dosent help:https://socket.io/docs/v3/client-initialization/
i dont get any error.
server.s
'use strict';
// Setup basic express server
var express = require('express');
var app = express();
var port = process.env.PORT || 4000;
var crypto = require('crypto');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./data/db.sqlite');
const bcrypt = require('bcrypt');
const tls = require('tls');
var validator = require('validator');
// Routing
app.use(express.static(__dirname + '/public'));
app.use(express.json());
// Chatroom
// usernames which are currently connected to the chat
var usernames = {};
var numUsers = 0;
const fs = require("fs");
const server = require("https").createServer({
cert: fs.readFileSync("./server-cert.pem"),
key: fs.readFileSync("./server-key.pem")
});
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
const io = require("socket.io")(server);
io.on('connection', function (socket) {
console.log("works")
}
client.js
const fs = require("fs");
const socket = require("socket.io-client")(4000, {
ca: fs.readFileSync("./server-cert.pem")
});
socket.on("connect_error", (err) => {
console.log(`connect_error due to ${err.message}`);
});

how to send data in post method from Javascript to nodejs server?

I am sending some data from my main.js to node js
here is my node js code
#index.js
var express = require('express');
var bodyParser = require('body-parser');
const app = express();
var cors = require('cors');
app.use(cors())
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.post('/listUsers', function (req, res) {
var t = (req.body.uuu);
console.log("C"+t);
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
and here is my Javascript code from where I am sending the data
usid();
function usid(med){
var f = "my new name";
var json_data = [];
json_data.push(f);
var uuu = JSON.stringify(json_data);
$.ajax({
crossDomain: true,
url:"localhost:8081/listUsers",
method:"POST",
data:{med,uuu:uuu},
success:function(data,status){
console.log("send");
}
})
}
Cannot GET /listUsers this the error in my webpage
can you tell me what I am doing wrong here ??thanks

Sending a GET request JSON object from server side to client side in Node.JS

I'm new Node.js and server side scripting in general and am currently practicing with the brewerydb-node wrapper found here(https://www.npmjs.com/package/brewerydb-node)
I currently have the following server side code that will log the appropriate JSON object to the command line
var express = require('express');
var app = express();
var morgan = require('morgan');
var bodyParser = require('body-parser');
var BreweryDb = require('brewerydb-node');
var brewdb = new BreweryDb([api-key here]);
var request = require('request');
app.use(bodyParser.json());
brewdb.breweries.getById("g0jHqt", {}, function(err, beer) {
if(err) {
console.log(res.statusCode());
} else {
console.log(beer.name);
}
})
app.listen(8000, function() {
console.log("Listening at http://localhost:8000");
})
I'm not sure how I would go about having this object be sent as a response to which I could parse through with my client side code as there are no 'res' or 'req' parameters in this wrapper.
You want to wrap your request in a route, like so:
var express = require('express');
var app = express();
var morgan = require('morgan');
var bodyParser = require('body-parser');
var BreweryDb = require('brewerydb-node');
var brewdb = new BreweryDb([api-key here]);
var request = require('request');
app.use(bodyParser.json());
app.get('/breweries/:id', function(req,res){
// in here a request to http://localhost:8000/breweries/g0jHqt will fetch the same as your example code
brewdb.breweries.getById(req.params.id, {}, function(err, beer) {
if(err) {
console.error(err);
res.status(500).send("An error occurred");
} else if(beer) { // we found the beer
res.send(beer);
} else{
res.status(404).send('We could not find your beer');
}
})
});
app.listen(8000, function() {
console.log("Listening at http://localhost:8000");
})

How to get my node.js mocha test running?

I have developed a service in node.js and looking to create my first ever mocha test for this in a seperate file test.js, so I can run the test like this:
mocha test
I could not figure out how to get the reference to my app, routes.js:
var _ = require('underscore');
module.exports = function (app) {
app.post('/*', function (req, res) {
var schema={
type: Object,
"schema":
{
"totalRecords": {type:Number}
}
};
var isvalid = require('isvalid');
var validJson=true;
isvalid(req.body,schema
, function(err, validObj) {
if (!validObj) {
validJson = false;
}
handleRequest(validJson,res,err,req);
});
})
}
This is the server.js:
// set up ======================================================================
var express = require('express');
var app = express(); // create our app w/ express
var port = process.env.PORT || 8080; // set the port
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use (function (error, req, res, next){
res.setHeader('content-type', 'application/json');
res.status(400);
res.json({
"error": "errormsg"
});
});
// routes ======================================================================
require('./routes.js')(app);
// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);
And finally test.js:
"use strict";
var request = require('supertest');
var assert = require('assert');
var express = require('express');
var app = express();
describe('testing filter', function() {
it('should return an error', function (done) {
request(app)
.post('/')
.send({"hh":"ss"})
.expect(400,{"error": "errormsg"})
.end(function (err, res) {
if (err) {
done(err);
} else {
done();
}
});
});
});
Create a separate file called app.js. The only purpose of this file would be to run the server. You'll also need to export your app object from server.js. So, your server.js would look like this
// set up ======================================================================
var express = require('express');
var app = express(); // create our app w/ express
var port = process.env.PORT || 8080; // set the port
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use (function (error, req, res, next){
res.setHeader('content-type', 'application/json');
res.status(400);
res.json({
"error": "errormsg"
});
});
// routes ======================================================================
require('./routes.js')(app);
module.exports = app;
Create a new file called app.js and put this inside of it
var app = require('./app');
var port = process.env.port || 8000;
app.listen(port, function() {
console.log("App listening on port " + port);
});
Now, inside of your test file, import your app as follows
var request = require('supertest');
var assert = require('assert');
var app = require('./app.js');
....
Note that I assume all your files are in the same directory. If you've put your test file in a different folder then you'll need to give the right path while requiring your app object.

express.Createserver() not working

I am beginner to nodejs. I tried below code. But server is not listening to the port and host.
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
var host = config.host;
var port = config.port;
var express = require("express") ,http = require("http");
var app = express();
var server = http.createServer(app);
app.get("/", function(request, response){
response.send("hello");
});
app.listen(port, host);
I also tried below code. But found in SO that this method is deprecated. And I replaced it with above code. Though it is not working. I also tried installing express globally then also not listening to port. What might be the problem.
var express = require("express");
var app = express.createServer();
app.get('/', function(request, response){
response.send("hello");
});
app.listen(port, host);
This is my config file.
[{
"host" : "127.0.0.1",
"port" : 1337
}]
config is array not object, you need to access it accordingly(or change the config) .Here is corrected code;
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
var host = config[0].host;
var port = config[0].port;
var express = require("express") ,http = require("http");
var app = express();
var server = http.createServer(app);
app.get("/", function(request, response){
response.send("hello");
});
var server = http.createServer(app);
app.get("/", function(request, response){
response.send("hello");
});
var server = app.listen(port, host);
server.on('error', function(err) {
console.log('error:' + err);
});
server.on('listening', function(){
console.log('server is up, all is well');
});

Categories