Node js file execution

I have a sample node js file that I executed on the command line, but it is not included in the browser,

var http = require('http'); port = process.argv[2] || 8888; http.createServer(function(request,response){ response.writeHead(200, { 'Content-Type': 'text/html' }); var PI = Math.PI; exports.area = function (r) { var res1 = PI * r * r; response.end(res1, 'utf-8'); // alert(res1); return res1; }; exports.circumference = function (r) { var res2 = 2 * PI * r; response.end(res2, 'utf-8'); //alert(res2); return res2; }; }).listen(parseInt(port, 10)); console.log("file server running at\n => hostname " + port + "/\nCTRL + C to shutdown"); 

strong text Can someone please tell me where I made a mistake.

+4
source share
3 answers

The problem is that you are not currently writing anything in response to the request.

response.write()

You also use methods like alert(); which are browser methods, but the code you execute is executed on the server side.

Currently, you are only declaring methods, but not calling anything.

This example should work:

 var http = require('http'); port = process.argv[2] || 8888; http.createServer(function(request, response) { response.writeHead(200, { 'Content-Type': 'text/html' }); var PI = Math.PI; area = function(r) { var res1 = PI * r * r; response.write('Area = ' + res1); // alert(res1); return res1; }; circumference = function(r) { var res2 = 2 * PI * r; response.write('circumference = ' +res2); //alert(res2); return res2; }; area(32); response.write(' '); circumference(23); response.end(); }).listen(parseInt(port, 10)); console.log("file server running at\n => hostname " + port + "/\nCTRL + C to shutdown"); 
+2
source

To extend my comment about alert does not work, here is how you could use express to do what you ask:

 var express = require('express'); var app = express(); app.configure(function(){ // I'll let you read the express API docs linked below to decide what you want to include here }); app.get('/area/:radius', function(req, res, next){ var r = req.params.radius; res.send(200, { area: Math.PI * r * r }); }); app.get('/circumference/:radius', function(req, res, next){ var r = req.params.radius; res.send(200, { circumference: 2 * Math.PI * r }); }); http.createServer(app).listen(8888, function(){ console.log('Listening on port 8888'); }); 

This assumes that you have included "express" in package.json and installed it with the npm installation. Here's the express API documentation .

+1
source

The problem is that you are not ending the response object, so your request continues and, ultimately, the failure and the need to complete the response object (with some data, if necessary)

 var http = require('http'); port = process.argv[2] || 8888; http.createServer(function(request,response){ var PI = Math.PI; exports.area = function (r) { var res1 = PI * r * r; alert(res1); return res1; }; exports.circumference = function (r) { var res2 = 2 * PI * r; alert(res2); return res2; }; response.end('hello'); }).listen(parseInt(port, 10)); console.log("file server running at\n => hostname " + port + "/\nCTRL + C to shutdown"); 
0
source

Source: https://habr.com/ru/post/1499355/


All Articles