How to create subpages with node.js using Amazon EC2 and heroku - javascript

I'm a beginner programmer and pretty new to Node.js.
I managed to setup a single static page by using AWS EC2 and Heroku, but I need help making other subpages. ie. mysite/blog or mysite/archive.
I started out with a simple web.js file I got from a sample node app which was:
var express = require('express');
var app = express();
app.use(express.logger());
app.get('/', function(request, response) {
response.send('Hello World!');
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
All that said was Hello World so I created index.html and changed web.js to this.
var express = require('express');
var fs = require('fs');
var htmlfile = "index.html";
var app = express(express.logger());
app.get('/', function(request, response) {
var html = fs.readFileSync(htmlfile).toString();
response.send(html);
});
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log("Listening on " + port);
Now that serves index.html instead, but how do I get /blog or /archive to work?

You can add other routes to your code to handle specific URL formats. Make sure the more specific URLs are listed before the general route (/ or *)
// Handle request for blog
app.get('/blog', function(req, res){
res.send('A list of blog posts should go here');
});
// Handle request for archive
app.get('/archive', function(req, res){
res.send('Archive content should go here');
});
// Route for everything else.
app.get('*', function(req, res){
res.send('Hello World');
});
I've got a more lengthy explanation about this here: http://hectorcorrea.com/#/blog/introduction-to-node-js/51

Related

How can I display a HTML file in app.post method in express js

var express = require('express');
var app = express();
var path = require('path');
var port = 3000;
app.use(express.static(__dirname));
app.get('/', (req, res) => {
res.send("Im fine");
});
app.get('/about' ,(req,res) =>{
res.sendFile(path.join(__dirname, 'modalbox.html'));
res.end();
})
app.listen(port, () =>{
console.log(`server running in https://localhost/${port}`);
})
modalbox.html isn't serving in "localhost:3000/about"
I can't find, why this html file isn't serving in this link, plz someone gimme the answer
trying to server the html file, but can't serve the file
Problem 1: The question is POST but the code is using GET.
Problem 2: Using res.end() end the response immediately while the file is being sent. res.sendFile is enough.
app.post('/about', (req, res) => {
res.sendFile(path.join(__dirname, 'modalbox.html'));
})
Problem 3: You've already serve static file using app.use(express.static(__dirname)). Just hit localhost:3000/modalbox.html and you can see your file

Showing index.html file with node.js

This is my first topic, and I'm not really that great with js, but how do I get my index.html file to show up? This is what I have.
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000
app.get("/", function (req, res) {
res.send("hello!")
});
app.listen(PORT, function () {
console.log("Server is running on localhost:" + PORT);
});
app.js and index.html are in the same directory. Thanks!
You can try using the following code into app.get function
res.sendFile(__dirname+/index.html");

Getting "Cannot GET /" in browser with express router

Working with express Router for getting for the first time.
This is my route.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('home page');
});
module.exports = router;
This is my index.js
var express = require('express');
var app = express();
var router=require('./route.js');
app.use('/route',router);
var server = app.listen(process.env.PORT ||8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port)
});
When I open run it in browser it shows:
Cannot GET /
The only URL the code you have written will respond to is:
www.example.com/route/
If you want it to respond to:
www.example.com
then change to the following in your index.js file:
app.use('/', router);
You should replace
app.use('/route',router); with app.use('/', router);
As I may see you have created default route in app.use as /route
you must not have added that using app.use('/') would be enough instead of creating another route for that
Thanks.

ExpressJs is not serving index.html

I have an Angular client application that runs entirely in the browser. I am trying to use expressjs to host it. I modeled the server code after the server.js in John Papa's MEAN Hot Towel application that he uses in his Pluralsight Gulp course. This is my server code:
var express = require('express');
var app = express();
var port = process.env.PORT || 7203;
var environment = process.env.NODE_ENV;
console.log('About to crank up node');
console.log('PORT=' + port);
console.log('NODE_ENV=' + environment);
app.get('/ping', function(req, res) {
console.log(req.body);
res.send('pong');
});
console.log('** DEV **');
app.use(express.static('./src/app'));
app.use(express.static('./'));
app.use(express.static('./temp'));
app.use('/*', express.static('./src/index.html'));
app.listen(port, function() {
console.log('Express server listening on port ' + port);
console.log('env = ' + app.get('env') +
'\n__dirname = ' + __dirname +
'\nprocess.cwd = ' + process.cwd());
});
When I navigate to localhost:port/ping, I get pong back. When I navigate to localhost:port/ I get a 404 error. Can someone tell me what I am doing wrong here?
As #tommyd456 stated in the comments above, you need to declare a route for '/', like so:
app.get('/', function(req, res) {
res.send('Hello!');
});
From the express documentation, it seems that express.static targets a folder.
So, replacing app.use('/*', express.static('./src/index.html')); by app.use('/*', express.static('./src')); fix your problem and index.html will be served under 'localhost:port/'
So after many hours of fiddling and reading I replaced this code:
app.use(express.static('./src/app'));
app.use(express.static('./'));
app.use(express.static('./temp'));
app.use('/*', express.static('./src/index.html'));
with this:
app.use(express.static(__dirname));
app.use(express.static(process.cwd()));
app.route('/*')
.get(function(req, res) {
res.sendFile(__dirname + '/index.html');
});
and it appears to have solved the problem

Getting Error when I deploy ext js app in node server

I am trying to deploy my ext js app in node server. Steps i have followed.
1.Created a Extjs app using sencha cmd and had used sencha app build to build my app
Once after building successfully i have taken my app in build-->production folder to my node server folder.
below screenshot contains dbview(client) files
When i start my node server and run my applicaiton using http://localhost:3000 getting following error in my console
Please find my server code
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.sendFile(__dirname+"\\dbview\\index.html");
})
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
Help me with the problem
You need to mount the directory so that express knows to look for static content there, or you could go the long way about it and create a specific route handler for that file:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname+"\\dbview\\index.html");
});
// THIS IS WHAT YOU NEED
app.use(express.static(__dirname));
// OR THE LONG WAY
app.get('/app.json', function(req, res) {
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
res.set('Content-Type', 'application/json');
res.sendFile('app.json', options);
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
See the Express Documentation for further details.

Categories