Here are the dependencies of my package.json file where i've added "cool-ascii-faces. I then need to update my index.js file to GET the /cool page so that on each reload I would see an ascii face. I'm getting a 404 error and it says 'Cannot GET /cool'
"dependencies": {
"ejs": "2.3.3",
"express": "4.13.3",
"cool-ascii-faces": "~1.3"
}
Below is my index.js file that calls declares cool
var cool = require('cool-ascii-faces');
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/cool', function(request, response) {
response.render('pages/index')
});
app.get('/cool', function(request, response) {
response.send(cool());
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
I then run npm install to update the dependencies and then heroku local, but get the 404 error.
Any help in the right direction would be great!
You're probably getting an exception when you start the node web server, due to a module dependency error.
Check your command/terminal window. If you see a red warning message pointing to your module.js file, you have an exception:
$ heroku local
forego | starting web.1 on port 5000
web.1 | module.js:341
In this case, you need to install the cool-ascii-faces module. In your 'node-js-getting-started' directory, use the following npm command to install:
$ npm i -S cool-ascii-faces
Also... you'll want to convert your index page route back to '/'. Your routes logic should look like this:
app.get('/', function(request, response) {
response.render('pages/index')
});
app.get('/cool', function(request, response) {
response.send(cool());
});
Otherwise, you'll always get the default 'pages/index' page when you hit the '/cool' route instead of a smiley face.
You don't have to include
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
}
Heroku will run "npm start" to start your server and dynamically choose the port. You don't have to specify port explicitly.
this problem happened for me. After I typed "git push heroku master", I typed "heroku open" and it worked.
Related
I'm trying to use express with Pug (Jade) to render/display a page and the function get() is coming back undefined with warnings...
I've run:
npm install express --save
npm install pug --save
Heres the top of the JS file with undefined get method...
var app = require('express')();
app.set('view engine', 'pug');
app.get('/', function (req, res) {
console.log("test"); //line isn't reached
res.render('index', { title: 'Hey', message: 'Hello there!' })
});
No errors when running, it just doesn't work.
You need to start the express server by
app.listen(9000, function(){
console.log('Server started...');
});
Add this line to the bottom of your JS file. Then open your browser and hit the url:
localhost:9000
Only then will the GET method call be invoked by express.
Hi I am currently trying to learn how to create a backend for my app but when trying to create a port 3000 and having curl it produces this error:
NodeTutorial git:(master) ✗ curl -v http://localhost:3000 - the connection is refused.
Please Help.
My code in my index.js is as follows:
var http = require('http'),
express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Seems your server has not started, try running below steps and see if you are able to connect or not:
Step 1: Create a directory which will contain all the files related to our app and execute npm init:
$ mkdir nodejs-server
$ npm init
Step 2: Install Express as a dependency:
$ cd nodejs-server
$ npm install --save express
Step 3: Create default entry point for Node.js i.e index.js, inside the same directory we created:
$ cd nodejs-server
$ touch index.js
Copy the content you have shown in your question in index.js:
var http = require('http'),
express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Step 4: Move to directory nodejs-server and start the app following below command:
$ cd nodejs-server
$ node index.js
I am using node.js to run locally (To start the server I am using: "node index.js" in my terminal) and I keep getting this error when I try to link my array.js file.
GET http://localhost:3000/array.js 404 (Not Found)
My file paths are as such if that matters...
/Campus/views(Index.pug file)
/Campus(Index.js file)
/Campus/public/js(array.js file)
My HTML(pug syntax) Header:
doctype html
head
script(src='array.js', type='text/javascript')
link(rel='stylesheet', type='text/css', href='http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css')
link(rel='stylesheet', type='text/css', href='http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css')
I feel that I did indeed reference my .js file correctly here, but this is my first time using node.js and maybe there is something I am over looking.
My Index.js code:
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 3000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', 'views');
app.set('view engine', 'pug');
app.get('/', function(request, response) {
response.render('index', {
title: 'Homepage'
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
You put it in public/js/array.js, but you're referencing it in public/array.js. Your path should be js/array.js
Documentation: https://expressjs.com/en/starter/static-files.html
I'm having a newbie problem with using express. I'm using Ubuntu 14.04 and make a new dir and run "express" in the terminal and it sets up a project template. Then run "npm install" to install the dependencies. And then edit 'views/layout.jade' to change "!!!" to "doctype html" as the node error suggests when trying to run the server without change.
Afterward, I start the server by entering "node app.js" on terminal and am then able to see the default page in my browser by using "localhost:3000" or "192.168.1.13:3000".
My understanding is that I should be able to use "192.168.1.13:3000" from another computer in the local network. However when I try this I get "connecting to 192.168.1.13..." for about 30 seconds to a minute then it says "The connection has timed out ... The server at 192.168.1.13 is taking too long to respond."
I've tried this from firefox and chrome on both windows desktop and android phone. I also tried setting it to port 80 instead of 3000 with the same result. I've tried adding "'0.0.0.0'," after the port in app.listen but this has same result as well.
I have never set up or messed around with firewalls or port forwarding in this Ubuntu installation so I believe those shouldn't be the issue? (is the problem with my router?) Maybe I'm wrong here.
Here's the relevant files-
app.js:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
routes/index.js:
/*
* GET home page.
*/
exports.index = function(req, res){
res.render('index', { title: 'Express' })
};
views/layout.jade:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
views/index.jade:
h1= title
p Welcome to #{title}
Let me know if I've left anything out. Cheers.
I resolved this by opening incoming port 3000 with iptables as suggested by slebetman, with the following command:
iptables -I INPUT 1 -p tcp --dport 3000 -j ACCEPT
I have started a node js project and up to now I have included my html pages/css/etc in a folder named 'html'. I have npm- installed the relevat modules also. But some error message is displayed during the launch. plz help me out.Thnx
Project Hierarchy
squadra-server.njs
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.configure(function () {
app.use(express.static(__dirname + '/html'));
})
app.listen(8000);
error msg
You have to configure the path to node.exe in your run configuration. Should not be much of a problem...