my web site sometimes raise 404 error even though there is correct path in my index file.
When occurs 404 error, I reload the page and the page correctly uploaded.
even though by reloading the page successfully page uploaded, I think it's not stable...
Do you have any idea? I don't know why it happens.
This is my route and handler to main page!
And I used vhost to connect domain
app1.get('/',(req,res)=>{
var arr = poplist;
var type = 'recommended';
var session = null;
if(req.session.user){
session = req.session;
}
if (req.session.series_id){ /*5*/
var search_car = req.session.series_id;
arr.unshift({'id':req.session.series_id,'series_name':req.session.manu_name + ' ' + req.session.series_name});
type = 'mycar';
} else {
var search_car = recommend_car(arr).id; /*4*/
}
get_recommend_container(arr,function(err,recommend_container){
if (err) { res.send('err') }
else {
get_best_reviews_container(function(err,best_reviews_container){
if (err) { res.send('err') }
else {
/*search_car for query in menubar*/
res.render('main', {'type':type,'poplist':poplist,'recommend_container':recommend_container,'best_reviews_container':best_reviews_container,'search_car':search_car,'session':session});
}
});
}
});
});
....
app.use(vhost('mysite.com', app1));
app.use(function(req, res, next) {
res.status(404).send('Sorry cannot find the page!');
});
app.listen(8000,'myip',function(){
console.log("Connected to 8000 port!..")
});
You explicitely define a "404 middleware" :
app.use(function(req, res, next) {
res.status(404).send('Sorry cannot find the page!');
});
This means that every time your app will receive any request, it will execute that middleware that will send back a 404.
Documentation : https://expressjs.com/en/guide/using-middleware.html
This example shows a middleware function with no mount path. The
function is executed every time the app receives a request.
var app = express()
app.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})
Related
I am trying to redirect the user with a post request from the home page after checking if their sessions exist.
This is my home controller file:-
const express = require('express');
const router = express.Router();
router.get('/', (req, res, next) => {
if (req.session["Data"] != undefined) {
res.redirect(307, '/Try');
}
else {res.render('home', {pageTitle: "Home"});}
});
module.exports = router;
But it is giving me error- Cannot GET /Try
This is what I'm using in my route file- router.post('/Try', try_controller.Try);
I am using res.redirect(307, '/Try') in another controller file of the same project and it's working. I can't figure out why it's not working here.
I don't think you can redirect a GET as a POST. If you own the /Try route, one option is to add a GET handler for that, then redirect will work.
Otherwise, in your GET route handler for \ you can create a new POST and return the results of that.
const request = require('request')
router.get('/', (req, res, next) => {
if (req.session["Data"] != undefined) {
//res.redirect(307, '/Try');
request.post('/Try', {}, function(err, response, body) {
if (err) return next(err)
return res.status(response.statusCode).send(body);
})
}
else {res.render('home', {pageTitle: "Home"});}
});
The example above an https://github.com/request/request though there are more modern ways of sending POST from express.
This isn't technically "redirecting", so you won't return 307 or 302.
I tried different things but in the end, I added an empty form in my home.pug file and submitted it using js.
JS code -
script.
let ssn = !{JSON.stringify(session)};
data = "Data"
if (ssn[data] != undefined) {document.getElementById('form-id').submit();}
I have the following node-mitm code.
mitm = Mitm();
mitm.on("request", function(req, res) {
const body = req.body; //body is null
})
I feel this has to do with reading node's IncomingMessage events, but I don't know how to do it.
Mitm.js's request handler is just like the one you're used to on Node's side. That is, it doesn't do anything special with req.body and leaves it as a ReadableStream.
You could either get its contents with the classical on("data") pattern:
mitm.on("request", function(req, res) {
req.on("data", function(data) { data == "Hello" })
})
If you want to fake a larger service, I've sometimes used Express to create routes and then pass Express's route handler to Mitm:
var Router = require("express").Router
var router = Router().use(require("body-parser").text())
router.get("/", function(req, res) { req.end() })
mitm.on("request", route.bind(null, router))
function route(router, req, res) {
router(req, res, function(err) {
if (err == null) return
res.writeHead(502)
throw err
})
}
The last example is a summary of the pattern I've also got publicly visible at the Rahvaalgatus open source repository: https://github.com/rahvaalgatus/rahvaalgatus.
Specifically, look at the controller test of https://github.com/rahvaalgatus/rahvaalgatus/blob/6dc91b026d75879cdc552bd2e63f220235b786c0/test/controllers/home_controller_test.js and see the this.router definition at https://github.com/rahvaalgatus/rahvaalgatus/blob/6dc91b026d75879cdc552bd2e63f220235b786c0/test/mitm.js.
I am trying to create a module which can log some certain params for the request and print them to the page which can be checked online, the page will use the socket.io to load the latest logs.
And I want this module can worked as a plugin which means you just call this module, and initialize it, then an extra entry point /_logger will be added to you application, once you visit the page, the latest logs will be updated in real-time. So the module have to intercept the requests:
function setup(httpServer) {
//page
httpServer.on("request", function (request, response) {
var pathname = url.parse(request.url).pathname;
if (pathname === '/_logger') {
fs.readFile(__dirname + '/logger.html', (err, data) => {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write(data);
response.end();
});
}else{
// how to give up the control for this requset
}
});
var io = require('socket.io')(httpServer);
io.on('connection', function (socket) {
//TO BE DONE
socket.on('event', function (data) { });
socket.on('disconnect', function () { });
});
}
module.exports = {
setup: setup
}
Usage:
var logger= require("./logger/index");
var server = require('http').createServer();
logger.setup(server);
server.on("request", function(req,res){
//Normal logic for different application
});
server.listen(3333);
Now the problem is that once the requested url is not /_logger, I should release the control of this request.
if (pathname === '/_logger') {
//take control
}else{
// Nothing should be done here, it should go to the next request chain.
}
After read the documents, I can not find the right way to make it.
Any ideas?
Assuming that you want to use low-level NodeJS HTTP API. You can compose several handlers into one handler using function composition. Each handler should yield the execution to the next handler, if the req.url doesn't matches.
var http = require('http');
var handler1 = function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('/');
res.end();
}
var handler2 = function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('/Hello');
res.end();
}
var middleware = compose([wrapHandler('/', handler1),
wrapHandler('/hello', handler2)]);
http.createServer(middleware).listen(3000);
function wrapHandler(path, cb) {
return function (req, res, next) {
if (req.url === path) {
cb(req, res);
} else {
next();
}
};
}
function notFoundHandler(req, res) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write('No Path found');
res.end();
};
// adapted from koa-compose
function compose(middleware) {
return function (req, res){
let next = function () {
notFoundHandler.call(this, req, res);
};
let i = middleware.length;
while (i--) {
let thisMiddleware = middleware[i];
let nextMiddleware = next;
next = function () {
thisMiddleware.call(this, req, res, nextMiddleware);
}
}
return next();
}
}
In your case, you can write.
var loggerHandler = wrapHandler('/_logger', logger.handler);
httpServer.on('request', compose(loggerHandler, handler2, handler3));
httpServer.on("request", ...) is just one request listener. It is under no obligation to process the request if it doesn't need to. Even if it does nothing, any other request listeners will still get notified of this request.
If there are other request listeners (which you are implying that there are), then you can just do nothing in the request listener you show and the other listeners will also get a shot at the particular request. This allows you to add your own request listener to a working http server and your listener only has to pay attention to the new route that it wants to support and can just ignore all the other routes and they will get handled by the other listeners that are already in place.
Now, there are frameworks built to make this both simpler and to give you more control. In general, these frameworks use one listener and they provide a means for you to handle the request OR explicitly tell the framework that you have not handled the request and would like other route handlers to have a shot at handling the request. This is a bit more flexible than just have multiple listeners, all of which will get notified of the same route.
For example, using the Express framework, you can do this:
var express = require('express');
var app = express();
// route handler for / request only when a user=xxx is in the query string
app.get('/', function(req, res, next) {
// if user was included in query parameter
if (req.query.user) {
// do something specific when ?user=xxxx is included in the URL
} else {
// pass handling to the next request handler in the chain
next();
}
});
// route handler for / request that wasn't already handled
app.get('/', function(req, res) {
// handle the / route here
});
app.listen(80);
I'm trying to add a configuration value that allows us to take down the site temporarily when needed. It works, but for some reason it is not rendering the error page I've defined in my 503 route. I'm seeing "Service Unavailable. Redirecting to /503", but it doesn't ever redirect and doesn't display the page. Can anyone tell me what I'm missing here?
Middleware:
// Maintenance mode
app.use(function (req, res, next) {
if (config.globals.maintenanceModeEnabled) {
// Need this condition to avoid redirect loop
if (req.url !== '/503') {
res.redirect(503, '/503');
} else {
next();
}
} else {
next();
}
});
Route:
// Down for maintenance page
router.get('/503', function (req, res) {
res.status(503).render('common/503', {layout: 'base'});
});
I've been trying to handle HTTP error in restify. these are the codes i've been trying so far :
function send403(req, res, err, cb) {
console.log('Forbidden');
res.end();
}
And then i use that function in ForbiddenError event :
server.on('ForbiddenError', send403);
I'm expecting that function to be executed everytime server receive a request without authorization header
server.get('/resource', function (req, res, next) {
if(typeof req.headers['authorization'] === 'undefined') {
return next(new restify.ForbiddenError());
}
else {
// HTTP 200
}
});
Everytime i try to access the url i keep getting that default restify JSON message.
FYI i've tried the same method for 404 error and it works
server.on('NotFound', send404);
Everytime i try to send a request to a non-existing url, the send404 function will be executed.
I think this might be what you are looking for. I noticed that in the "server.on" statement restify doesn't want the "Error" part. The following is a complete working example.
'use strict';
var restify = require('restify');
var server = restify.createServer();
server.on('Forbidden', send403);
server.get('/resource', function(req, res, next){
if(typeof req.headers['authorization'] === 'undefined') {
next(new restify.ForbiddenError());
}
else {
// HTTP 200
}
});
function send403(req, res, err, cb){
console.log('Log something here');
return cb();
}
server.listen(9000);
console.log('listening on port 9000');