I have created a server with node js but I have a big mistake.
I use the library HTTP for receive request from port 3000 I try to get the URL of the request.
So I try localhost:3000/dashboard and I perfectly read /dashboard
Now I try localhost:3000/data and I receive /
WHY ?
here is my code:
var http = require('http');
var server = http.createServer();
server.on('request', function(req,res){
console.log('CALL : '+req.url);
res.end();
});
server.listen(3000);`
thanks for help me
Related
I am using http-proxy to proxy my requests. I am using https not http. The first request is a login request and works fine. The second request is to connect to socket.io which doesn't works even doesn't show any error. I have backend server which listens at port 3000 and handles both requests. Where is something wrong with in my code? The socket.io connection doesn't get established, even no error or anything is printed in the console to know root cause of the problem. I think it doesn't even gets upgraded. What do I do to make it work?
var app = require('express')();
const https = require('https');
const fs = require('fs');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({ws:true});
var server=https.createServer(credentials, app).listen(8082)
app.use('/login', function (req, res) {
console.log("Request Url "+req.url)
proxy.web(req, res,{target:'example.com:3000'})
})
app.use('/socket.io',function(req,socket,head){
console.log("Request Url "+req.url)
server.on('upgrade', function(req, socket, head) {
socket.on('error', (error) => {
console.error(error);
});
proxy.ws(req, socket, head, {target:'example.com:3000'})
});
})
What I have tried
ws:true, secure:true options while proxying requests. Error eventlistener doesn't print any errors. Any help would be appreciated.
Node.js: v10.16.0
Operating System: Windows 10
Question: My simple test server can send messages, but not html information. Why would that be?
Background: The following simple server displays the message 'hello world' in the browser. However when I check the Network tab in devtools I see an error regarding favicon.ico (failed)::ERR_CONNECTION_REFUSED.
'use strict';
const http = require('http');
const port = 3000;
const server = http.createServer((req,res) => {
res.end('hello world');
process.exit();
});
server.listen(port);
However when I attempt to send html information it throws an error immediately. This site can’t be reached localhost refused to connect. ERR_CONNECTION_REFUSED.
'use strict';
const http = require('http');
const port = 3000;
const server = http.createServer((req,res) => {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<header><title>hello world</title></header>');
res.write('<body><h1>hello world</h1></body>');
res.write('</html>');
res.end();
process.exit();
});
server.listen(port);
The server is so simple and I'm following very basic tutorials. I'm not sure why favicon.ico is throwing a connection refused error in the first and why the second version fails immediately.
Browsers usually send multiple requests such as favion.ico, actual page etc., for HTML Pages.
Your code has process.exit() after receiving the first request.
You can either remove that line like below and it should work.
'use strict';
const http = require('http');
const port = 3000;
const server = http.createServer((req,res) => {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<header><title>hello world</title></header>');
res.write('<body><h1>hello world</h1></body>');
res.write('</html>');
res.end();
});
server.listen(port);
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello Http');
});
server.listen(8080);
I am not getting result for the above nodejs code i am executing the code like this node hw.js from git bash
I am new to nodejs
Your code is creating an HTTP server on port 8080.
For every request it will respond with 'Hello Http'
Try opening http://localhost:8080/ in your browser and you will see it.
I've done this from scratch and it still gives me an error...
I've run
express test
then
cd test && npm install
I've edited the app.js adding a route such this:
app.get('/test',function(req,res) {
res.writeHead(200, {"Content-Type": "application/json"});
return res.send('{"a":3}');
});
Then I've run node
node app.js
And when I try to access http://server/test I get
Error: Can't set headers after they are sent.
I'm using
Node v4.2.1, Express 2.5.8, npm 3.4.0.
This just happens with Express, if I create a simple server on Node I can use writeHead.
When res.writeHead(200, {"Content-Type": "application/json"});,you send all the response headers to the client,so you can not send header again.
Because res.send is the function of express's response object,I look the source code of this send function:
chunk is what you send here : String('{"a":3}')
this.set('Content-Type', setCharset(type, 'utf-8')) here express helps us concat our content-type with utf-8 so server needs to send header again
That is why you get the error.
Ps.
Sorry for my bad english and I hope you understand what I try to explain.
What about using res.setHeader
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.setHeader("Content-Type","application/json");
res.send('{}');
});
var server = app.listen(3000, function () {
var host = "localhost";
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
I am learning Node.JS and this is the most commonly available example of server by Node.JS
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
// var name=request.getParameter('name');
// console.log(name);
console.log('res: ' + JSON.stringify(response.body));
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
Now when I am executing this from console it works fine, and from browser also it works fine, by hitting the URL: localhost:8000
But now I also want to send some parameters to this server, so I tried localhost:8000/?name=John and few more URL's but none of them work, Can anyone help me?
Thanks in advance!!
try:
var url = require('url');
var name = url.parse(request.url, true).query['name'];
Node's HTTP API is rather low-level compared to other frameworks/environments that you might be familiar with, so pleasantries like a getParameter() method don't exist out of the box.
You can get the query-string from the request's url, which you can then parse:
var http = require('http');
var url = require('url');
var server = http.createServer(function (request, response) {
var parsedUrl = url.parse(request.url, true);
var query = parsedUrl.query;
console.log(query.name);
// ...
});