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!
Related
I can connect them both when my NodeJS server is deployed on localhost PORT, for example
const PORT = 9000;
const app = express()
app.listen(PORT, () => console.log(`Server is running successfully on PORT ${PORT}`))
app.use(bodyParser.json({extended: true}))
app.use(cors())
app.use(bodyParser.urlencoded({extended: true}))
app.use('/', router)
And in my front end, I can do the following:
const url = 'http://localhost:9000'
...
const res = await axios.get(`${url}/post/${path}`)
This is just an example.
But what if I wanted to deploy my NodeJS server into a heroku application, for example randomname.herokuapp.com, and I want to do
const url = 'http://randomname.herokuapp.com:9000'
...
const res = await axios.get(`${url}/post/${path}`)
It obviously doesn't work. So I'd appreciate anyone who can help me do this.
Hello first of all if you deploy an app on heroku you will have to change port,you should add this to your code
app.listen(process.env.PORT || 3000,function(){
console.log("Server started at ${PORT}");
});
because heroku will not deploy on the same port as you did on your localhost.
Furthermore if i understand your question to link front with back end,in your code you should start building paths like
app.get("/")
app.post("/")
to handle get and post request to your home root and any other root.
Also in your front pages you need to take user input,if you want text field,checkbox or something else in a form and take them in your back end ,if it is on your home page for example.
app.post("/home",function(req,res){
const input=req.body.name;
}
I have also deployed apps on heroku and the main idea still remains same,you handle get,post request with these commands.You only change port and Pocfile to run your app on heroku.Get and post routes still remain same ("/home ,/info").
The link for your app is going to be:
"https://something_given_from_heroku.com" ,which will be given automatically from heroku.And you hit other routes like /home
I hope this help you,but check other posts to be sure.
I've been doing this myself, instead of starting with barebones up to skeleton and onwards just download this base web app which you can immediately deploy to Heroku.. Link is https://github.com/hubspot/basewebapp
from there, initiate a repo on github from that link and deploy on Heroku via deployment and with automatic deploys, change on VSC or GitHub, changes apply to the webpage/webapp on Heroku.
If you need any other help, feel free to contact me.. Been surfing Heroku apps for a few weeks already.
Check www.conid.dev for as far as I've published so far
var express = require('express');
var app = express();
app.get("/", function(req,res){
console.log("running on that weird path i made///");
res.send("HELLO THIS IS A WEBPAGE TRYOUT!");
});
app.listen(3000, function(){
console.log("Server running on 3000");
})
I’m trying to create a simple app (for practice) which does a simple hello world. Using express via node.js
I can do it locally no problems.
The problem is I’m trying to run it on an existing server using cPanel shared hosting.
And there is obviously an already running domain name(and has website installed in Wordpress), so I cannot do a simple app.listen to a port which is obviously already running. And I’m guessing therefor I cannot do app.get(“/“....)
I’ve tried a different port-but the get request won’t work(I think obviously since that’s not the one running?)
So when I tell it to listen to the cPanel port, it throws an error that it’s already running.
And I don’t want to stop from the website to run. Is there a way to work this?
Tried also using subdomains. Same result.
Edit:
this worked on postman, when I've sent a get request to that address. tried port 3000, and it showed my res.send on the postman tab.
Edit2:
I've solved this...i forgot to put :3000 on the address..
as in www.example.com:3000
this doesn't work if i don't add the :3000.
and also if i do process.env.PORT and .IP..guessing its because its a shared hosting, or because wordpress is installed...
I won't delete just in case anyone did the same mistake as me.
If the problem that the port is already in use, you may try to use 0 as a port that will cause it to take a random free port on the machine.
const server = app.listen(0, function(){
console.log(`Server is listening on http://localhost:${server.address().port}`);
})
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
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!
I’m encountering an error that I’ve never seen before when I run “gcloud app logs read” after deploying my app using "gcloud app deploy".
This is the error: “ERROR: gcloud crashed (ValueError): time data '2016-09-16T19:39:42Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'”
I thought it might be because of some changes I made to the app, but I tried deploying a simple server and I’m still getting the same error when I try to look at the logs. Again, it does seem to be deploying, so I’m having trouble figuring out what the problem is with the logging functionality.
IOW, no matter what I deploy, I get the same error.
I get the same error when I deploy this:
'use strict';
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.status(200).send('Hello, world!');
});
// Start the server
var server = app.listen(process.env.PORT || '8080', function () {
console.log('App listening on port %s', server.address().port);
console.log('Press Ctrl+C to quit.');
});
If anyone has any ideas, it would be much appreciated. Thank you.
Edit: Added code.
This is related to a known issue and should be fixed shortly. You can check the logs in the Cloud Console, or using the gcloud beta logging read command instead in the meantime.
Update: we have a fix expected in the next Cloud SDK release.
Update: Cloud SDK 127.0.0 is out; please update your client and this should be resolved.
'2016-09-16T19:39:42Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
Somewhere in your code, you are asking to show a datetime string with microseconds: .%f. But, the datetime element you are providing does not have the microseconds. Either remove the .%f, or make the datetime string have microseconds.