1HttpClient client = HttpClient.newHttpClient();
2HttpRequest request = HttpRequest.newBuilder()
3 .uri(URI.create("http://openjdk.java.net/"))
4 .build();
5client.sendAsync(request, BodyHandlers.ofString())
6 .thenApply(HttpResponse::body)
7 .thenAccept(System.out::println)
8 .join();
1var http = require('http'); // Import Node.js core module
2
3var server = http.createServer(function (req, res) { //create web server
4 if (req.url == '/') { //check the URL of the current request
5
6 // set response header
7 res.writeHead(200, { 'Content-Type': 'text/html' });
8
9 // set response content
10 res.write('<html><body><p>This is home Page.</p></body></html>');
11 res.end();
12
13 }
14 else if (req.url == "/student") {
15
16 res.writeHead(200, { 'Content-Type': 'text/html' });
17 res.write('<html><body><p>This is student Page.</p></body></html>');
18 res.end();
19
20 }
21 else if (req.url == "/admin") {
22
23 res.writeHead(200, { 'Content-Type': 'text/html' });
24 res.write('<html><body><p>This is admin Page.</p></body></html>');
25 res.end();
26
27 }
28 else
29 res.end('Invalid Request!');
30
31});
32
33server.listen(5000); //6 - listen for any incoming requests
34
35console.log('Node.js web server at port 5000 is running..')
36
1/* ====== create node.js server with core 'http' module ====== */
2// dependencies
3const http = require("http");
4
5// PORT
6const PORT = 3000;
7
8// server create
9const server = http.createServer((req, res) => {
10 if (req.url === "/") {
11 res.write("This is home page.");
12 res.end();
13 } else if (req.url === "/about" && req.method === "GET") {
14 res.write("This is about page.");
15 res.end();
16 } else {
17 res.write("Not Found!");
18 res.end();
19 }
20});
21
22// server listen port
23server.listen(PORT);
24
25console.log(`Server is running on PORT: ${PORT}`);
26
27// ======== Instructions ========
28// save this as index.js
29// you have to download and install node.js on your machine
30// open terminal or command prompt
31// type node index.js
32// find your server at http://localhost:3000
1// code by VARSHITH REDDY SATTI
2// to create a server in node.js you should.
3var http = require('http');
4http.createServer(function (req, res) {
5 res.writeHead(200, {'Content-Type': 'text/html'});
6 res.write("write html code to display you test")
7 res.end();
8}).listen(8080);
9// save this as httpServer.js
10// run this by typing node httpServer.js in the command line
11// to acess your server got to http://localhost:8080
1var http = require('http');
2var fs = require('fs');
3var path = require('path');
4
5http.createServer(function (request, response) {
6 console.log('request ', request.url);
7
8 var filePath = '.' + request.url;
9 if (filePath == './') {
10 filePath = './index.html';
11 }
12
13 var extname = String(path.extname(filePath)).toLowerCase();
14 var mimeTypes = {
15 '.html': 'text/html',
16 '.js': 'text/javascript',
17 '.css': 'text/css',
18 '.json': 'application/json',
19 '.png': 'image/png',
20 '.jpg': 'image/jpg',
21 '.gif': 'image/gif',
22 '.svg': 'image/svg+xml',
23 '.wav': 'audio/wav',
24 '.mp4': 'video/mp4',
25 '.woff': 'application/font-woff',
26 '.ttf': 'application/font-ttf',
27 '.eot': 'application/vnd.ms-fontobject',
28 '.otf': 'application/font-otf',
29 '.wasm': 'application/wasm'
30 };
31
32 var contentType = mimeTypes[extname] || 'application/octet-stream';
33
34 fs.readFile(filePath, function(error, content) {
35 if (error) {
36 if(error.code == 'ENOENT') {
37 fs.readFile('./404.html', function(error, content) {
38 response.writeHead(404, { 'Content-Type': 'text/html' });
39 response.end(content, 'utf-8');
40 });
41 }
42 else {
43 response.writeHead(500);
44 response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
45 }
46 }
47 else {
48 response.writeHead(200, { 'Content-Type': contentType });
49 response.end(content, 'utf-8');
50 }
51 });
52
53}).listen(8125);
54console.log('Server running at http://127.0.0.1:8125/');