app.get("/test", function(req, res, next) {
res.sendFile(__dirname + '/test.html');
});
Pretty basic, yes? Running this server on my Mac this works fine. Running this server on a PC my browser receives "cannot GET" error. I don't think the issue is the permissions on the mp4 file, they seem to be ok.
Sorry guys, not sure what I did differently but it is working now!
For the record, here is the full app:
var express = require('express');
var app = express();
app.use(express.static('dist'));
app.get("/demo", function(req, res, next) {
res.setHeader('Content-Type', 'video/mp4');
res.sendFile(__dirname + '/demo.mp4');
});
var server = app.listen(80, function () {});
Related
I have created an express angular app which works fine but if I am at a URL say http://localhost:4007/login and hit refresh it gets an error tried many things to correct it nothing works.
My server snippet code is as follow.
app.use(express.static(path.join(__dirname, '../client/dist/bringclean')));;
app.get('/*', function (req, res){
res.redirect('/');
});
app.listen(4007, function () {
console.log('BringClean app is running on port 4007');
});
Found a way to solve the problem.
const routes = require('express').Router();
routes.get('*', function (req, res) {
res.sendFile(path.join(__dirname + '/../client/dist/projectname/index.html'));
});
I have the following express server setup:
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
console.log('/*');
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/test', function (req, res) {
console.log('test');
//res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
Everytime I hit localhost:9000/test, it still take to the route '/'.
I tried placing the app.get code before the static declaration but I don't think that really makes any difference. Any idea why this is happening?
Your existing code is fine. It should work.
I believe you had your index route as app.get('/*', function (req, res){ instead of app.get('/', function (req, res) { before and that's why it was capturing all requests.
In the /test route handler you have to terminate the request-response cycle by using res.end(), something like this:
app.get('/test', function (req, res) {
console.log('test');
res.end();
});
Otherwise the page will never refresh showing the first page (route /)
Hope this helps
I'm running a node.js rest api with Express v4. All the routes work as expected via localhost debug. But on my server, they don't. The only thing that differs, is that the port listening is done by Phusion Passenger on the server (I can't change that). Here's my app.js:
const express = require('express'),
bodyParser = require('body-parser'),
cors = require('cors'),
helmet = require('helmet'),
http = require('http');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use(helmet());
app.use('/', require('./api/routes'));
var server = http.createServer(app);
var port = 'passenger'; // 3000 on localhost
server.listen(port, function () {
console.log('listening ' + port);
});
Here's a my routes.js that works on local debug, but won't on the server:
var express = require('express');
var router = express.Router();
router.get('/api/test', function (req, res, next) {
console.log(req.route.path);
console.log(req.route.methods);
console.log('test get works !');
res.end('test get works!');
});
router.post('/api/test', function (req, res, next) {
console.log(req.route.path);
console.log(req.route.methods);
console.log('test post works !');
res.end('test post works!');
});
module.exports = router;
Here are the logs for localhost (successful routing):
20180107_192254 | /api/test
20180107_192254 | { get: true }
20180107_192254 | test get works !
20180107_192350 | /api/test
20180107_192350 | { post: true }
20180107_192350 | test post works !
The log file of the same process, but done via the remote server (I'm always redirected to the first route, and req.method is always GET). While the 'req.route.path' is the always the good one, it never goes into the expected function:
20180107_192757 | /api/test
20180107_192757 | { get: true }
20180107_192757 | test get works !
20180107_192759 | /api/test
20180107_192759 | { get: true }
20180107_192759 | test get works !
Do you know if I have to update something somewhere (maybe .htaccess, or in the js code)? I couldn't find anything on both the Phusion and the Express doc.
EDIT:
I replace the express router with a direct use of route by the app and it doesn't work.
But I noticed something:
var express = require('express');
var router = express.Router();
router.get('/api/test', function (req, res, next) {
console.log(req.route.path);
console.log(req.route.methods);
console.log('test get works !');
res.end('test get works!');
});
router.post('/api/test', function (req, res, next) {
console.log(req.route.path);
console.log(req.route.methods);
console.log('test post works !');
res.end('test post works!');
});
router.get('/api/otherTest', function (req, res, next) {
console.log(req.route.path);
console.log(req.route.methods);
console.log('other test get works !');
res.end('other test get works!');
});
module.exports = router;
Here, I can go the the first and the third routes. In fact, all GET routes work (and only them). If I do this:
var express = require('express');
var router = express.Router();
router.post('/api/test', function (req, res, next) {
console.log(req.route.path);
console.log(req.route.methods);
console.log('test post works !');
res.end('test post works!');
});
module.exports = router;
I get nothing.
Ok. So I figure it out: my website is rerouting all http:// traffic to https:// traffic, and all requests (GET, POST, PUT,...) sent through http:// are somehow transformed to GET requests.
They are all correct via https://.
I don't know why this is happening, but feel free to give more details about this issue if you do! I'll update when I find out.
i have following problem in my express application for now. I have different services with build in webserver running. The problem is now i need to access it following:
localhost:8888
The normal server can be accessed over localhost:3000.
Is it possible to create something like a proxy to have something like this.
localhost:3000
localhost:3000/admin/chronograf
localhost:3000/admin/mongo
const express = require('express');
const httpProxy = require('http-proxy');
const app = express();
const proxyOptions = {
}
const apiProxy = httpProxy.createProxyServer(proxyOptions);
const serverOne = 'http://localhost:8888';
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.all("/app1", function(req, res) {
console.log('redirecting to Server1');
apiProxy.web(req, res, {target: serverOne});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
The Problem is that the page is showing put the links of the resources are not rewritten to the new path. I need a config value or some own Implementation to rewrite the urls.
thanks for helping
I am using express to serve a single page webapp, and as a REST endpoint for said app. Everything works fine for the static page serving, but after 5 posts the server becomes unresponsive. I have seen a lot of other posts with this problem but they all say to just make sure to call res.send() or res.end() which i do in every call somewhere, regardless of how the logic branches. The app.js code is below.
/**
* Module dependencies.
*/
var express = require('express'),
http = require('http'),
path = require('path');
var app = express();
var auth = require("./controllers/authentication.js");
http.globalAgent.maxSockets = 100;
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', function(req, res) {
res.sendfile('./public/index.html');
});
app.get('/public/*', function(req, res) {
res.sendfile('.'+req.url);
});
app.post('/auth/login', function(req, res, next) {
auth.login(req, res, next);
});
app.post('/auth/logout', function(req, res, next) {
auth.logout(req, res, next);
});
app.post('/auth/verify', function(req, res, next) {
auth.verify(req, res, next, function(req, res, next) {
res.conentType = 'json';
res.send(200, "authorized");
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
and here is the command line output that i get (i tried issuing other requests after, but the server would not process them). I assume that I am somehow not terminating the connection properly, but cant figure out how.
problem was related to not closing mysql connection pool