Serve an HTML file in node.js - javascript

app.get('/',(req,res)=>{
res.send("index.html ");});
app.listen(3000);
console.log("Server Started");
whenever i enter my local host IP address it displays "index.html" text instead of opening index.html file

You are trying to serve an HTML file, so you have to res.sendFile().
var express = require('express');
var app = express();
var path = require('path');
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(3000);

Related

react app with express.js route setup

I was using angular.js and I do this in express.js
app.get("*", function (req, res) {
res.redirect('/#' + req.originalUrl)
})
so that the browser will use the route of angular instead of express. But how to do that with react router? I have 2 folder, named server and client, server folder has express and api logic while client folder simply a react app.
You need to put the path in of the HTML file you are rendering your app to
app.get("/",(res,res) => {
res.sendFile('put the path of the html file you are rendering your app into here');
}
here is an example of a express server.js that works with react
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var app = express();
var PORT = process.env.PORT || 3000;
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.text());
app.use(bodyParser.json({type: 'application/vnd.api+json'}));
app.use(express.static('./public'));
app.get('/', function(req,res){
res.sendFile('./public/index.html');
});
app.listen(PORT, function(){
console.log("Listening on port: " + PORT);
});

Unable to render index.html file from my view folder in angular2

I'm creating a MEAN Stack application where angular has setup in /client folder. I want that when I run npm start command in /client folder it should render index.html file from /views folder, what I'm doing wrong getting this error
Cannot GET /
Folder structure is as follows.
meanApp
----- client (angluar2 setup here but doesn't have an index.html file)
---------- app
----- views
----------index.html
----- routes
----- server.js
Codes in server.js
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var index = require('./routes/index');
var tasks = require("./routes/tasks");
var app = express();
//View engines
app.set("views", path.join(__dirname,'views'));
app.set("view engine", 'ejs');
app.engine("html", require("ejs").renderFile);
//Set static folder
app.use(express.static(path.join(__dirname,'client')));
// Body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use('/', index);
app.use('/index', index);
app.use('/api', tasks);
//listen
app.listen(3000, function(){
console.log("Server listing # 3000");
});
Here you need to define route for express server like :
app.set('appPath', 'client'); //this is a folder where your index.html is
app.route('/*')
.get(function(req, res) {
res.sendfile(app.get('appPath') + '/index.html');
});
This will cause every call in broweser to render index file.
const http = require('http');
fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var app = express();
app.set('appPath', 'views');
app.use(express.static(__dirname + '/views'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(expressValidator());
app.use('/*', function(req, res, next) {
res.sendfile(app.get('appPath') + '/index.html');
});
http.createServer(app).listen(3001, function() {
console.log(`Express server listening on port 3001`);
});
exports = module.exports = app;

express.static not working

I have the below code
var express = require('express');
var app = express();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});
app.use(express.static(__dirname + '/public'));
http.listen(3000, function(){
console.log('listening on *:3000');
});
But when executing localhost:3000/Home.html where Home.html is the html file inside my public directory its saying Cannot GET /Home.html.
I dont know what mistake i did.
Please correct me.

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

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

Static Content Serving Not Working In Express

var express = require('express');
var app = express();
app.get('/', function(req, res){
app.use(express.static('../../www'))
})
app.listen(8080)
according to docs this should work but it just returns a page of garbled text
It's better to use path module to join the current folder and relative path to an absolute path.
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, '../../www')));
app.get('/', function(req, res){
res.send('done');
});
app.listen(8080);
As #bulkan comments, you use /style.css to access www/style.css.
Move the app.use(express.static('../../www')); outside of the app.get like so;
var express = require('express');
var app = express();
app.use(express.static('../../www'));
app.get('/', function(req, res){
res.send('done');
});
app.listen(8080);
http://expressjs.com/api.html#app.use

Categories