im writing a webserver in nodeJs as seen in the following code. I got the server working but sadly i cant acces the website outside my network (localhost). How can i do this?
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(80, function(){
console.log('Server running on 80...');
});
I also used this method to check if the other method was the problem.
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res){
fs.readFile('Index.html', function (err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
})
}).listen(80);
Have you tried binding the port externally ?
connect().use(serveStatic(__dirname)).listen({
host: '0.0.0.0',
port: 80
}, function(){
console.log('Server running on 80...');
});
Related
I installed node on my web host but when I enter for example :
www.example.com:3000/server.js
it's showing me the file content instead of executing it
and giving me timeout!
Source:
const http = require('http');
const port = 3000;
const server = http.createServer(function (req, res){
res.write('Hello node')
res.end
})
server.listen(port,function (error){
if (error) {
console.log('err',error)
}else {
console.log('server port',port)
}
})
the output from the website:
const http = require('http');
const port = 3000;
const server = http.createServer(function (req, res){
res.write('Hello node')
res.end
})
server.listen(port,function (error){
if (error) {
console.log('err',error)
}else {
console.log('server port',port)
}
})
The output from the console:
server port 3000
What should the output be:
Hello node
note:
I did lunch it with node and entered the ports*
So the answer is when I called the support of godaddy.com I gave him the issue, after checking my hosting he says that my hosting plan not supporting root or super user, and node.js requires superuser privileges, so by the end i want to thank all of the people who tried to help me, thank you so much.
I am learning node.js and I am trying to figure out how I can get my program to listen at a specific port, but I just keep getting error messages. Is there any easier way I could be doing this, or what do I need to change in my code to allow this to work?
const http = require('http');
const port = 3000
const requestHandler = (request, response) => {
console.log(request.url)
response.end('server is listening!')
}
const server = http.creatServer(requestHandler)
server.listen(port, (err) => ) {
console.log('server is listening on ${port}')
}
I think you should try something like the following to get started.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(8080);
Or in your case, I think that the following changes will work:
const http = require('http');
const port = 3000
const requestHandler = (request, response) => {
console.log(request.url)
response.end('server is listening!')
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
console.log('server is listening on ${port}')
})
It seems you had a syntax error as well as a typo in your code.
I'm wondering how to listen to http get requests with only "require http" instead o f express.
This is what I have now:
let http = require('http');
let server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello, World!\n');
});
server.listen(8443);
console.log('Server running on port 8443');
I want to listen to get requests, and console.log the url. and if there is any other request i want to print ("bad request").
You need to check what method was used using http: message.method and if it is not GET then send another response.
'use strict'
let http = require('http');
let server = http.createServer(function (req, res) {
if( req.method === 'GET' ) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello, World!\n');
} else {
res.writeHead(405, {'Content-Type': 'text/plain'});
res.end('Method Not Allowed\n');
}
});
server.listen(8443);
console.log('Server running on port 8443');
I want to display all the output from pokecli.py on a web page that can be accessed from http://192.168.32.100:8081. Currently I am getting a "connection refused" from Chrome, but no errors when running node myscript.js.
I am new to Node and I am not exactly sure if this is right. I want to display the output in real time. I know this is possible even without NGINX since I can get output from the following example code by opening http://192.168.32.100:8080:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, '192.168.0.251');
console.log('Server running at http://192.168.0.251:8080/');
Here is my code:
var http = require('http');
var PythonShell = require('python-shell');
var express = require('express');
var app = express();
// Options to be used by request
var options = {
host: '127.0.0.1',
port: '8081'
};
// Callback function is used to deal with response
var callback = function(response){
// Continuously update stream with data
var body = '';
response.on('data', function(data) {
body += data;
PythonShell.run('pokecli.py', function (err) {
if (err) throw err;
console.log('finished');
});
});
response.on('end', function() {
// Data received completely.
console.log(body);
});
}
// Make a request to the server
var req = http.request(options, callback);
app.get('/', function (req, res) {
res.send('Hello World!'); // This will serve your request to '/'.
});
app.listen(8081, function () {
console.log('Example app listening on port 8081!');
});
req.end();
I have a Problem with my Node.js Server. I want to host a html Document but there is one Problem with a TypeError. I cann't find the mistake. Can you help me?
var express = require("express");
var mysql = require('mysql');
var app = express();
var fs = require("fs");
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : 'root',
password : '',
database : 'test',
debug : false
});
app.get('/', function(req, res) {
fs.readFile('index.html', 'utf-8',function (err, data){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
});
app.listen(3000);
console.log("SERVER IS NOW RUNNING AT PORT 3000........");
Here now the log:
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" Server.js
SERVER IS NOW RUNNING AT PORT 3000........
_http_outgoing.js:430
throw new TypeError('first argument must be a string or Buffer');
^
TypeError: first argument must be a string or Buffer
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:430:11)
at ReadFileContext.callback (c:\........\Server.js:21:13)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:303:13)
Later I want to make a MySQL Connection with Pooling.
You can also use createReadStream and .pipe to res but as #robertklep mentioned, you should check if (err) inside the readFile callback.
Here is the example
var fs = require('fs');
var http = require('http');
var file = fs.createReadStream('/path/to/file');
var server = http.createServer(function (req, res) {
// res.writeHead(200, {
// 'content-type': 'text/plain'
// });
file.pipe(res);
});
server.listen('3000');
UPDATE
So to match your code using express, you don't have to do much:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(3000, function () {
console.log('SERVER IS NOW RUNNING AT PORT 3000........');
});
Just put all your assets in the public folder and you should be good to go. To find more info about Express you can go to http://expressjs.com/en/4x/api.html