0
Q:

nodejs http server

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("http://openjdk.java.net/"))
      .build();
client.sendAsync(request, BodyHandlers.ofString())
      .thenApply(HttpResponse::body)
      .thenAccept(System.out::println)
      .join();
1
const http = require("http");

const server = http.createServer((req, res) => {
  console.log(req.url);

  if (req.url === "/") {
    res.write("<h1>This is home page</h1>");
  } else if (req.url === "/about") {
    res.write("<h1> This is about page </h1>");
  } else if (req.url === "/contact") {
    res.write("<h1>This is contact page</h1>");
  } else {
    res.write("<h1>Page not found</h1>");
  }
  
  res.end();
});

server.listen(3000, () => {
  console.log("server is running");
});
1
const http = require('http');
const port = 3000;

console.log(`Server is running on port ${port}`);
http.createServer(function(req, res){
	res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('Write some HTML here...');
    res.end();
}).listen(port);

// save this as server.js
// you have to download node.js
// open terminal or command prompt
// type node server.js
// find your server at http://localhost:3000
// to end the server, go to that terminal and press ctrl+c
2
// code by VARSHITH REDDY SATTI
// to create a server in node.js you should.
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write("write html code to display you test")
  res.end();
}).listen(8080);
// save this as httpServer.js
// run this by typing node httpServer.js in the command line
// to acess your server got to http://localhost:8080
2
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 
2
var http = require('http');
var fs = require('fs');
var path = require('path');

http.createServer(function (request, response) {
    console.log('request ', request.url);

    var filePath = '.' + request.url;
    if (filePath == './') {
        filePath = './index.html';
    }

    var extname = String(path.extname(filePath)).toLowerCase();
    var mimeTypes = {
        '.html': 'text/html',
        '.js': 'text/javascript',
        '.css': 'text/css',
        '.json': 'application/json',
        '.png': 'image/png',
        '.jpg': 'image/jpg',
        '.gif': 'image/gif',
        '.svg': 'image/svg+xml',
        '.wav': 'audio/wav',
        '.mp4': 'video/mp4',
        '.woff': 'application/font-woff',
        '.ttf': 'application/font-ttf',
        '.eot': 'application/vnd.ms-fontobject',
        '.otf': 'application/font-otf',
        '.wasm': 'application/wasm'
    };

    var contentType = mimeTypes[extname] || 'application/octet-stream';

    fs.readFile(filePath, function(error, content) {
        if (error) {
            if(error.code == 'ENOENT') {
                fs.readFile('./404.html', function(error, content) {
                    response.writeHead(404, { 'Content-Type': 'text/html' });
                    response.end(content, 'utf-8');
                });
            }
            else {
                response.writeHead(500);
                response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
            }
        }
        else {
            response.writeHead(200, { 'Content-Type': contentType });
            response.end(content, 'utf-8');
        }
    });

}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
0
const http = require("http");
const port = 3000; // Replace this if you want, this is just used for most examples

http.createServer("/", (req, res) => {
  res.writeHead(200);
  res.write("Hello World");
  res.end();
});

http.listen(port, console.log(`Server started at http://localhost:${port}`));
-1

New to Communities?

Join the community