I'm a newbie to express, and I have my app which looks like this:
I have my code like this:
const express = require("express"),
app = express(),
server = require("http").Server(app),
io = require("socket.io")(server)
app.use(express.static(__dirname + "/sign-in/public"))
app.use(express.static(__dirname + "/log-in/public"))
app.get("/sign-in", (req, res) => res.sendFile(__dirname + "/sign-in/public/index.html"))
app.get("/log-in", (req, res) => res.sendFile(__dirname + "/log-in/public/index.html"))
server.listen(8081, () => console.log(`Listening on ${server.address().port}`))
But the problem is, after have executed node server.js, when I go to localhost:8081/log-in, I'll see the sign-up page, same with localhost:8081 and localhost:8081/sign-up. But, I wanna have the log-in page when I go to localhost:8081/log-in... How can I achieve that? What did I do wrong? Thanks
The problem is that you are serving both static path under the same path. Your code should look like this:
const express = require("express"),
app = express();
app.use('/sign-in/', express.static(__dirname + "/sign-in/public"));
app.use('/log-in/', express.static(__dirname + "/log-in/public"));
app.listen(8081, () => console.log(`Listening on 8081`));
Try this it will work fine
app.get('/login', function(req,res){
res.sendfile(__dirname + 'login/public/index.html');
});
Then the reason you are getting this error is because You have defined one Static file route you cannot define it Again this might be the problem i think so i worked 6 months on Mean Stack
Related
I'm learning express.js at the moment. Rigth now, my root "/" path is not working anymore. When I check localhost:3000/ nothing is displayed, just a blank page. I can't figure out why. When I use other pathes like e.g. "/hello" its working. Strangely I copied the code from an udemy lessons and its the exact same code. In the udemy lessons its working.
Where is my mistake? What did I do wrong?
I want localhost:3000/ to display my response "Hello"
const express = require('express');
const path = require('path');
const hoganMiddleware = require('hogan-middleware');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'mustache')
app.engine('mustache', hoganMiddleware.__express)
app.use(express.static(path.join(__dirname, 'public')))
app.get('/hello' , (req, res, next) => {
res.send('Hello from hello');
})
app.get('/', (req, res, next) => {
res.send('Hello')
})
app.listen(3000, () => {
console.log('server is running on ' + 3000);
});
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
I'm using node.js, express, and ejs as a development environment. I've run into an issue where my main page isn't rendered. However, other pages are rendered. I get absolutely no return when I access http://127.0.0.1:9999. However, I get the proper response when I access http://127.0.0.1:9999/about. I replaced my normal page with a very simple test page to check if there was something wrong it. Nope. No change. I can only conclude that the path '/' (or '') isn't seeing the request.
Can anyone see the problem? Thanks
app.js
const path = require("path");
const express = require("express");
const ejs = require("ejs");
const app = express();
const port = 9999;
const viewsPath = path.join(__dirname, "./views");
app.set("view engine", "ejs");
app.set("views", viewsPath);
app.use(express.static(path.join(__dirname, "/public")));
app.get("/", function(req, res) {
console.log("index accessed");
res.status(200).render("partials/test.ejs");
});
app.get("/about", function(req, res) {
console.log("about accessed");
res.status(200).render("partials/test.ejs");
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
test.ejs
<h1>This is a test page</h1>
I added the following route, and the path is not matched.
app.get("*", function(req, res) {
console.log("* accessed");
res.status(200).render("partials/test.ejs");
});
In the meanwhile it is possible to use:
app.use(express.static(app_path, {
index: false
}));
Credits to
Im new at nodejs programming and im having a little problem now. When i try to go localhost:3000/ i want to go homeController and index function prints the HTML file.
APP.JS
const express = require('express')
const mongoose = require('mongoose')
const mongodb = require('mongodb')
const app = express();
const homeController = require('./home/homeController.js');
app.get('/', function(req, res) {
res.redirect(homeController.index);
});
app.listen(3000, () => console.log('Example app listening on port 80!'))
HOMECONTROLLER.JS
var path = require("path");
exports.index = function(req, res){
res.sendFile(path.join(__dirname+'/index.html'));
};
console.log('test33');
Also i am using exports to seperate app.js from other controllers. Is this the right way? I have a history with Python Django framework and we used to use URLs to navigate our program.
Thanks.
OUTPUT
Cannot GET
/function%20(req,%20res)%7B%0A%20%20res.sendFile(path.join(__dirname+'/index.html'));%0A%7D
Your problem is that homeController.index is a function, but you're not calling it. Replace:
app.get('/', function(req, res) {
res.redirect(homeController.index);
});
with:
app.get('/', homeController.index);
Your homeController.js exports an index function which requires two parameters reqand res.
So you have to update your app.js accordingly :
app.get('/', function(req, res) {
homeController.index(req, res);
});
edit: by the way, your app is listening to port 3000
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