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.
Related
So I am very new to node.js and I am on a mac. I am learning from W3schools and I have to run myfirst.js in the local host but I keep getting this error:
This page isn’t working localhost didn’t send any data.
ERR_EMPTY_RESPONSE
here is the code:
const https = require('https');
https.createServer(function (req,res){
res.writeHead(200, {'content-type':'text/html'});
res.send('Hello world!');
}).listen(8080);
and I ran this code in the terminal:
Macs-MBP:~ mac$ node /Users/mac/test/myfirst.js
the terminal command works but when i go to my browser and run :http://localhost:8080
I just keep getting the above error
how would I fix this?
Try it like this:
const http = require('http');
var server = http.createServer(function (req,res){
res.writeHead(200, {'content-type':'text/html'});
res.write('<html><body><p>Hello world!</p></body></html>');
res.end();
});
server.listen(8080);
console.log('Node.js web server running..')
I'm trying to run index.js in localhost.
So, I make index.js like this.
const http = require('http');
http.createServer((req, res) => {
res.end('Hello');
}).listen(3000, ()=> {
console.log('running');
})
I typed node index.js but there is nothing in the console and it just turns off by itself.
There is nothing on console.
like this.
I typed that thing at PowerShell and git bash but none of them works.
I tried another port like 8080 or 8000 but it's not working.
And I also used forever, pm2, and nodemon but it doesn't work, too.
I think it's something like a firewall problem.
But I don't know what's wrong.
Try to break your code.
It might be because your code does not hold the instance of the server, it clear itself ... hence the listen promise isn't even reached.
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello');
})
server.listen(3000, ()=> {
console.log('running');
})
## Try below code please
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
I have a small node program.
console.log("Program Started");
var http = require('http');
//Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
console.log("Processing Request for URL" + request.url);
response.writeHead(200, {"Content-Type": "text/html"});
response.end("Hello World \n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
console.log("Program Ended");
If I run this program on my MacBook and access the following URL, I see my Hello World message.
http://localhost:8000/
However, I'd like to have this URL responding to HTTPS connections as well.
https://localhost:8000/
How can I do this with NodeJS?
Can the native http library do this for me? Or will I need Express?
Yes you can simply do It via https module :
1 you will have to create certificats , you can follow this auto here for macOS
https://stackoverflow.com/a/42298344/8395557
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
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);
});