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
Related
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}`);
});
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)
})
I have a problem with my nodejs server. Here is my code
server.js
global.jQuery = global.$ = require('jquery');
var express = require('express'),
path = require('path'),
menu = require("./routes/menu");
var sql = require("mssql");
var http = require("http");
var io2 = require("io");
var app = express();
var serve = http.createServer(app);
var io = require('socket.io')(serve);
var recordset2;
var port = 8080;
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine','jade');
/* app.use(express.favicon());*/
app.use(app.router);
app.use(express.static(path.join(__dirname+'public')));
});
app.get('/:viewname', menu.viewname);
io.on('connection', function (socket) {
socket.on('disconnect', function () {
console.log('user disconnected');
});
socket.on('chat', function (msg) {
socket.broadcast.emit('chat', msg);
});
});
var dbConfig = {
server: "localhost",
database: "MyDatabase",
user: "sa",
password: "sa",
port: 1433
};
function getConnected() {
var conn = new sql.Connection(dbConfig);
conn.connect().then(function () {
var req = new sql.Request(conn);
req.query("SELECT * FROM Countries").then(function (recordset) {
console.log("Recordset:", recordset);
conn.close();
}).catch(function (err) {
console.log("Error!!!!");
console.log(err);
conn.close();
});
}).catch(function (err) {
console.log("Error!!!! ----");
console.log(err);
});
}
getConnected();
app.listen(port);
client's code
var socket = io();
$(function () {
$('#get-button').on('click', function () {
console.log("CLICK");
var msg = "HIIII";
socket.emit('chat', msg);
});
});
I'm trying to make a connection between server and client by socket, but it returns me the following error:
Error: Failed to lookup view 'socket.io' in views directory C:\Radio/views;
at Function.app.render (C:\Radio\node_modules\express\lib\application.js:493:17)
at ServerResponse.res.render (C:\Radio\node_modules\express\lib\response.js:798:7)
at exports.viewname (C:\Radio\routes\menu.js:2:9)
at callbacks (C:\Radio\node_modules\express\lib\router\index.js:164:37)
at param (C:\Radio\node_modules\express\lib\router\index.js:138:11)
at param (C:\Radio\node_modules\express\lib\router\index.js:135:11)
at pass (C:\Radio\node_modules\express\lib\router\index.js:145:5)
at Router._dispatch (C:\Radio\node_modules\express\lib\router\index.js:173:5)
at Object.router (C:\Radio\node_modules\express\lib\router\index.js:33:10)
at next (C:\Radio\node_modules\express\node_modules\connect\lib\proto.js:193:15
Could you help me understand why? I saw there is another similar topics but none of the solutions there helped.
I found a solution two minutes after posting this topic.
global.jQuery = global.$ = require('jquery');
var express = require('express'),
path = require('path'),
menu = require("./routes/menu");
var sql = require("mssql");
var http = require("http");
var io2 = require("io");
var app = express();
var port = 8080;
var ser = app.listen(port); //<-----------This solved my problem.
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");
})
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');
});