Hey guys I am trying to learn node.js and angular.js. When I start the node.js program via npm start on a package.json that runs node on the www file. I can load the index page but when I kill it in the console and then try starting it again I cannot access the same port number because it says it was in use.
Interesting. Can you share come additional information?
OS, Development Environment, Port number.
As soon as I've got that, I've got a few bits of advice.
The code looks good, so it looks like your node environment isn't properly closing the network socket.
run netstat -awt
and check if something is listening on port 5005.
What distro are you running?
On an unrelated note, you are including the node 6.6 package in your git repository. You shouldn't do that. List the dependencies on your readme.md and let the people installing it configure their environment however they like.
well I am running Mint and the port I previously ran on was port 5000 and somthing is listening on that port so I need that opened up so I can use it.
Let me try to get this straight.
You ran the program once on port 5000 and it worked.
You killed it and tried to run it again on 5000 and it didn't work.
You moved it to 5005 temporarily.
yes sir
Alright. In that case, your system is not recognizing the port being free, or the process is still running. Are you 100% node isn't still running?
I am doing Ctrl + z and I cannot connect to that port via web browser.
does open it after restart.
in the terminal you used, type fg
then Ctrl + c. From there, try starting it again.
What Ctrl + z does is it pauses the process and sends it to the background. So you haven't actually freed up the port, because there's still a socket connected to it, it's just not responding.
fg
brings the process back to the foreground.
One last question, I have used ASP.NET MVC 5 and through that I was able to render different pages via giving the name of the view and then it would render the .cshtml pages. I was wondering how I render the different views via web browser and where in .js is that configured?
If you're asking how to "assign" a view to a url, like setting GET /users
to render views/users.jade
You'd do that in the routes
directory.
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
could you clarify what this means?
Sure.
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
//here, you're telling the "router" which is basically code to
//handle incoming requests to take any GET requests for "/" and
//handle it with the following anonymous function (the whole function(req, res, next) { thing
res.render('index', { title: 'Express' });
// here, you're telling the system to render the index view, from the "views" directory
// and you're passing the jade renderer a dict with data where the title field contains Express.
});
module.exports = router;
You seem pretty new to node and express, so I'll recommend you watch this tutorial series.
Bucky has some great resources on his channel and it's where I've learned all my basic knowledge about most programming languages.