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
Related
I have a simple react app made with CRA, and I deployed it on vercel. Everything seems to be working with one exception. My route handlers are not sending back the correct response. It seems like they are sending the index.html file that is created from the build script. I'm not really sure why this is happening. It sent the correct response when I built it locally. Here is the code for the express server:
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, "build")));
app.get("/ping", function (req, res) {
return res.send("pong");
});
// app.get("*", (req, res) => {
// res.sendFile(path.join(__dirname, "build", "index.html"));
// });
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
And here is the file structure if that helps:
I figured out the issue. Vercel only supports serverless deployment. You can't host an express server, just a static site
tldr
Ok i've been trying to get a file upload server working for a few days now and evrything i try just returns cannot get.
i'm currently trying the setup below but it is not working
code
here is server.js
const express = require("express");
const upload = require("./upload");
const cors = require("cors");
var router = express.Router();
var app = express();
const server = express();
var corsOptions = {
origin: "*",
optionsSuccessStatus: 200
};
server.use(cors(corsOptions));
router.get("/", function(req, res) {
res.render("index", { title: "Express" });
});
server.post("/upload", upload);
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
Below is upload.js
const IncomingForm = require("formidable").IncomingForm;
module.exports = function upload(req, res) { var form = new IncomingForm();
form.on("file", (field, file) => {
// Do something with the file
// e.g. save it to the database
// you can access it using file.path
console.log("thisno werk"); }); form.on("end", () => {
res.json(); }); form.parse(req); };
Let's mean server as the result of the const server = require('express')();, and router as the result of const router = require('express').Router();.
server is an instance of your Express server while router is an instance of API endpoints router. You don't only need to write your router router.get();, but you also need to set the appropriate files (so-called controllers) for handling API requests.
So your code should have this line: server.use('/', yourController); or simply server.get('/', handlingFunction); if you don't have API sections.
However, if you use routers, then you need the first variant.
Your POST /upload method works great because it's configured on the app level. But you need to fix your GET / method because it's configured on the router level that is unused in your app.
Source: Express routing
Hy there, I'm trying to learn to make a REST API. I have the following code, where I mention that i have no error. When I try to access localhost:3000 nothing happens it's just reloading a blank page. What am I doing wrong?
Servers.js
const http = require('http');
const app = require('./app').default;
const port = process.env.port || 3000;
const server = http.createServer();
server.listen(port);
App.js
const express = require('express');
const app = express();
app.use((req, res, next) => {
res.status(200).json({
message: 'It works!'
});
});
module.exports = app;
You've not defined any route. Express generator generates the boilerplate code which is use full to start with.
You can also define a route like this:
app.use('/', function (req, res, next) {
return res.render('index', {title: 'DASHBOARD', 'data': ''});
});
Have a look at this doc: https://expressjs.com/en/starter/generator.html
var express = require('express')
var app = express()
var httpProxy = require('http-proxy')
var proxy = httpProxy.createProxyServer({});
app.use(function (req, res, next) {
if (req.host === dev.app.com) {
proxy.web(req, res, { target: req.url })
} else {
// don't proxy request
// just render the page
}
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
This is what I have for now, I tried reading through express js docs, but fail to understand on how to do this.
If I understood well, I believe you should just call res.render('your-page-here'); (In the case if you are using a template engine, like Pug or ejs)
Or, if you are going to serve a static page, res.sendFile(__dirname + '/index.html'); for example.
Following is my code as follows:
var express = require('express');
var app = express();
var router = express.Router();
app.route('/')
.get(function(req, res) {
res.send('Hello World!');
});
app.route('/user')
.get(function (req, res) {
res.send('Hello' + req.params.id);
});
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
It runs fine with http://localhost:8000/ but with http://localhost:8000/user?id=D it gives following error: Cannot GET /user?id=D.
WHat is wrong in my code? Please help.
Thanks.
This route syntax:
'/user/:id'
matches a URL like this:
http://localhost:8000/user/4095
If you use a URL like this:
http://localhost:8000/user?id=D
then, you will need to use a "/user" route and read the query parameter for the id value from req.query.id as described here.
In addition, your don't need the app.route() as it's just an extra level of complication for things you are not doing here. I'd suggest this simplification which I have tested and works:
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.get('/user', function (req, res) {
res.send('Hello: ' + req.query.id);
});
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});