ExpressJS-Localhost not showing anything - node.js

This is my first time I am running ExpressJS. But the Localhost is not responding any data.
Below is the Code I have written
const express=require('express');
const app=express();
app.get('/',(req,res)=>
{
});
app.listen(3000,()=>console.log("running at 3000 port"));
I have tried to change the localhost to 4000,5000.But same error.
Additional Information:
Running on Ubuntu 16.10
Any clue of whats happening over here?

You're not giving the browser anything to render. Try following the Hello World example in the docs and you might be pleasantly surprised:
https://expressjs.com/en/starter/hello-world.html

I dont know if you still need an answer but i think i might have it.
I had the same issue with the 'app.use(express.json)'
when i deleted my server started working, it was because we were writing it work it actually is
'app.use(express.json())'
PD: that command works for telling EXPRESS that it has to read a json file or request...
i hope this can help you out

Just to add to imjared's answer.. Always, response has to be sent back to the client(Browser) for any new page you create because that's what will be displayed on the browser. Adding res.send("some data") in the function will resolve the issue.

Related

AngularJS, nodeJS, ReferenceError: require is not defined

I have read several similar questions here on SO but couldn't find any answer that fits my case.
I am new to angularjs and nodejs, and I am stuck here:
I would like to store the input of a form into a json file. For that I found out about this procedure:
var fs = require('fs');
fs.appendFile('../database/lexicon.json', ' This is my text.', function (err) {
if (err) throw err;
console.log('Updated!');
});
I am just testing here with 'This is my text', and my json file is located here ../database/lexicon.json
But the issue comes from the fact that my program doesn't recognise the require.
I understand that it is a server side action and that it can not run in the browser, but I thought I would overcome that issue by running my app in a local server with this command:
npx http-server -o
Can anyone help me out with this issue? Thanks!
OK, I solved the problem, not with this example, but I moved on to another feature of my app that require another require and I don't get the error message anymore after installing express.js !
I hope this will help others.

Unable to connect to Express welcome page after install

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!

Does console.log not work within socket.io event?

Is there something that prevents a console.log() from firing within a socket.io event? I am having a frustrating time with them.
socket.on('messageReceived', function(data) {
console.log('Server ' + data.msg);
var clientData = {'msg', 'Hello server'};
socket.emit('clientResponse', clientData);
});
in that code my node server receives the client response, but my browser console does not output the console.log. I don't get why... is this normal?
You can try console.log('a') in console of browser.
If you don't see a, I think you changed level log of console (verbore, info, warn, error) or used filter (you can change it in under of tab console).
I don't know what happen if console priint a.
I figured out the problem. I was using socket.broadcast.emit from the server instead of broadcast.emit.
I noticed when another browser I had open outputted the console.log.
Thanks everyone who read and put thought into this.
I also had console.log not working and I have been spending the weekend debugging it. Nothing worked. (But my server was still running fine) Finally, I became fed up with it and I commented out my entire io.on('connection') code and it still ran.
Then, I went to my client and I saw that I still had my client sending and retrieving to and from my Heroku build that I set when I published the website.
So if you're having issues and nothing that you do is working, check the socket server that you're connecting to with your client.

Calling server-side code from client on Derby.js

I'm new to using Derby.js, and have scaffolded out a project using the yeoman generator-derby package. I thought everything was going fine, but I can't figure out what I'm doing wrong here.
A breakdown:
I have an 'app/dbWp.js' controller that exports several functions, and requires the modules 'mysql', 'async', and 'needle'
In my app/index.js, I import this file and use it like so:
app.proto.submitWp = function() {
dbWp.createUser(this.model);
};
I call this function from the view/index.jade like so:
button.btn.btn-primary(type="button", on-click="submitWp()")
In the browser, I get numerous console.error message complaining about the 'fs' module not being defined. After much googling, I discover that it's due to Browserify ignoring the 'fs' module, which subsequently causes problems with modules 'mysql' and 'needle'. But that implies this code is being executed in the browser?
So my question is: why is this trying to call the function on the client side? Obviously if it executes on the server side, as I thought it was going to, there wouldn't be a problem requiring these modules.
How can I execute this function on the server? Had this working fine with express + socket.io before, but wanted to change frameworks and give Derby.js a shot.
I'm clearly misunderstanding something about how Derby.js is supposed to work; any help would be appreciated. Thanks!
I know this is like 4 months later, but being new to DerbyJs too, I thought I could try and help.
I personally with standard html code have the equivalent working.
<button on-click="editContact(#contact.id)">Edit Contact</button>
This indeed runs code on the server. Can you try writing your code in standard HTML, or perhaps better yet, see if you can do a console.log on the server method to see if it even is getting there?
Perhaps the best would be to call an empty function on the server with a console log and check both the browser console and the server console.

Async profiling nodejs server to review the code?

We encountered performance problem on our nodejs server holding 100k ip everyday.
Now we want to review the code and find the bottle-neck.
#jfriend00 from what we can see now, the problem seems to be DB access and file access. But we don't know what logic caused this access.
We are still looking for good ways to do the async profiling of nodejs server.
Here's what we tried
Nodetime
This works for us to some extent. It can give the executing time of code specified to the lines. However, we can't locate the error because the server works async and no stacking and calling info can be determined.
Async-profiling
This works with async and is said to be the first of this kind.
Problem is, we've integrated it's js code with our server-side code.
var AsyncProfile = require('async-profile')
AsyncProfile.profile(function () {
///// OUR SERVER-SIDE CODE RESIDES HERE
setTimeout(function () {
// doAsyncStuff
});
});
We can only record the profile of one time of server execution for one request. Can we use this code with things like forever? I've no idea with this.
dtrace
This is too general for us to locate problem in nodejs code.
Do you have any idea on profiling nodejs server code? Any hints or suggestions are appreciated. Thanks.

Resources