I want to use the browser as a "2nd Console" for debugging purposes. My plan is to use the main console for the technical stuff and to use the browser to output App related data. Eventually I will pretty it all up, but for now, I just want to have it up and running.
So here is my quick and sloppy code:
const express = require('express')
const app = express()
var test;
app.all('/', (req, res) => res.send(test));
app.listen(3000, () => console.log('Example app listening on port 3000!'))
That's it, I just change the test var to whatever I want to show, it was working well for a while, but I'm getting to the point where there is a lot of data I want to show and it's getting sloppy.
I'd love to hear some suggestions.
Node features a debugger. To have a look into the internals of your app, why not use the debugger?
Start your node app with:
node --inspect server.js
This will give you websocket url (localhost by default) you can connect to.
Open chrome://inspect in a Chromium-based browser. Click the Configure button and ensure your target host and port are listed.
As an advantage: You will see ALL variables you have. There are other IDEs that are supported as well (including VS and VS Code).
For detailed instructions, more command line switches and setup the Inspector Interface see the Debugging Guide
Related
Ok, I've searched around a bunch, and maybe the answer is here and I just couldn't find it because I wasn't using the right search combination or whatever, so please bear with me!
I'm very new to Node and all the tutorials I've read/watched (understandably) use localhost with a port number for examples. I'm having difficulty translating that into a real, shared-server website.
I successfully got Node.js installed on my shared hosting on Siteground (I can check versions, run javascript by pulling up node through putty, etc.).
I can start an http server using port 3000 and my ip address, and I can confirm that the server listener is working using another putty window and curl ipaddressORdomainname: 3000 (for testing purposes until I get it connected). I'm running the following to start the server:
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.url, req.method);
});
server.listen(3000, 'my_ip_address', () => {
console.log('listening for requests on port 3000')
})
And running curl ipaddressORdomainname: 3000 in another putty window successfully displays / GET in the other putty window with the open port.
What I can't seem to do is use any web browser (tried latest versions of Brave, Chrome, Edge, Firefox, Opera to no avail) to "connect" and show the same result I get from the curl line.
I'm ultimately trying to connect a website to the node.js server, and I think this is my final missing piece I can't seem to find a solution to.
Any guidance would be very appreciated.
Thanks!
I am a total newbie and have a problem I just can't find the answer to. I followed the steps on this page: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/development_environment and I'm not able to connect to the sample pages with express. The hellonode.js connects fine when I run that.
Both of the express examples connect when I start the server and I get the 'example app listening...' message but when I go to the page in any browser I get a connection refused err. I thought maybe it could be firewall related so I disabled that, but that didn't help. I also tried going through the steps multiple times with different directory names, tried changing the port numbers and tried rebooting the machine. I always get the same result.
At this point I think I've just run out of talent and can't think of anything else to try. When I look at the package.json files they match what is shown on the page. Hopefully I'm missing something obvious, I just can't see it.
Edit:
The code I've used is from the page....first one is:
const express = require('express')
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.listen(8000, () => {
console.log('Example app listening on port 8000!')
});
and the second spot I tried and got the listening message but couldn't connect via a browser was after installing the express application generator and then running:
DEBUG=helloworld:* npm start
Edit 2:
If anyone else ever has this problem, I got it to work by following the instructions on this page instead. https://learn.microsoft.com/en-us/windows/nodejs/beginners#types-of-nodejs-applications Now to figure out why...
Hello Nick and welcome to Stack Overflow!
What you did is probably installing the packages you had missing befor with npm install.
Or maybe you were not using the correct syntax to set that environment variable DEBUG on your machine.
If I can suggest, avoid using express-generator, it's much better to learn how to do that manually, it's just a couple of lines of code anyways!
I created a free server from Google Cloud. I want to run Node.js on this server. I installed Node.js. I installed Express and created a project. Then I run the project (working).
But I can not get the output by typing ip-address: 3000. "This site can not be reached." I get as a result.
What could be the reason for that.
I just setup one to see how this works, how i setup the server, hope this helps:
var express = require('express');
var app = express();
app.get('/', (req, res) => {
res.json('Home Page');
})
app.listen(8080);
The web applications must listen for HTTP requests on ports within the permitted range 8080 to 8084.....To connect to a web application running on an instance, click the Web Preview button Web Preview Button above the Cloud Shell terminal window in the GCP Console. src: https://cloud.google.com/shell/docs/features#web_preview
Ok I am making website and want to use mongo, express, etc. I setup a server using fedora server ISO. The problem is getting node working. I have followed several tutorials, and its all the same. Nothing works. So I have to be doing something wrong. Trying to get the simplest thing to display on screen.
I think the server is running httpd server, whatever fedora has built in. I get the default fedora server page when going to the url. So the server is running and working, just hasn't been configured. When running node on the server do I have to use httpd-node? Or can it be http, etc.
Here is my app.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
And then I have a basic index.html that should be rendered just saying test.
I ssh into the server and run node start, it runs and the console logs the message like it should. But if I go to the address 192.168.1.5, or the domain that points to the server, I get nothing, just a blank page.
If someone can help me get this working, I can actually get to work coding out the application. Any help would be appreciated.
I think you make a confusion. When you build an Express application, you do not need another server at all.
When you start your app with:
app.listen(3000, function () {})
Express returns an http.Server object listening to port 3000.
When you navigate to your local adress on port 3000, you will see your "hello world" message.
It is possible that httpd service is already running in your Fedora environnement on default port 80 (the default port for http, the one your reach when you go to your local adress) but this is an Apache server and you do not need it to run your Nodejs app.
To build a Nodejs server, you can also use httpd-node package, but this is redundant as you're using Express framework.
If you need to serve a simple html file, a method I like for its simplicity is to use ejs template engine, something like this.
res.send('Hello World!') - this is your problem! Why?
How you receive this answer on client side?
Solution: use res.render(..) - for rendering from server or use AJAX on client side for receive this text!
P.S: render page and you don't see blank page anymore! Or use client-server conversation logic with your server through AJAX.
Try 192.168.1.5:3000
If I wrong: show your full project setup...
Test your app with curl (https://curl.haxx.se)! Check connection establishment, and show results here!
So I run a bunch of a little chatbots written in node, nothing too exciting. However, I recently decided to give them their own little web page to display information in a graphical manner. To do this, I figured I'd just run express.
However, I'm running my bots with a wrapper file that starts each chatbot as a child process. Which makes using express a little tricky. Currently I'm starting the express server in the wrapper.js file like so:
var express = require("express");
var web = express();
web.listen(3001);
And then in the child processes, I'm doing this:
var express = require("express");
var web = express();
web.get("/urlforbot",function (req,res) {
res.send("Working!");
});
However, when I navigate to :3001/urlforbot, I get Cannot GET /urlforbot.
Any idea what I'm doing wrong and how to fix this?
Edit: This is my complete wrapper file: http://snippi.com/s/3vn56m2
Edit 2: This is what I'm doing now. I'm hosting each bot on it's own port, and storing that information in the configs. This is the code I'm using, and it appears to be working:
web.get("/"+cfg.route, function (req,res) { // forward the data
res.redirect('http://url.com:'+cfg.port+"/"+cfg.route);
});
Since your bots run as separate processes (any particular reason?), you have to treat each one as having to implement their own HTTP server with Express:
var express = require("express");
var web = express();
web.get("/urlforbot",function (req,res) {
res.send("Working!");
});
web.listen(UNIQUE_PORT_NUMBER);
Each bot process needs to listen on a unique port number, it can't be shared.
Next, you need to map requests coming in on port 3001 in the 'master' process to the correct child process' Express server.
node-http-proxy has a useful option called a ProxyTable with which to create such a mapping, but it requires the master process to know what the endpoint (/urlforbot in your terms) for each bot is. It also requires that the master knows on which port the bots are listening.
EDIT: alternatively, you can use child_process.fork to fork a new process for each of your bots, and communicate between them and the master process (port numbers and such, or even all the data required to generate the /urlforbot pages) using the comm channel that Node provides, but that still sounds like an overly complex setup.
Wouldn't it be possible to create a Bot class instead? You'd instantiate the class for each bot you want to run, and that instance loads its specific configuration and adds its routes to the Express server. All from the same process.