I am trying to run Node.js on a domain that I do NOT own (basically a snadbox), I figured the problem is that socket.io and express are not running. The domain I have access to upload files to is https://'Domain'.com/MyID (domain is in quotes so it doesn't make a link here)
The code shown is what runs perfectly on localhost NOT ON A DOMAIN, this is my first experience with node thus I do not know what to do after weeks of videos that only show it running on localhost.
var express = require('express');
var app = express();
var serv = require('http').Server(app);
app.get('/',function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(2000);
console.log("Server started.");
So I found this
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
And I'm wandering if I can set hostname to a domain directory (directory being my access to edit /MyID)
Before edit:
I tried changing the requirements in the code above but that did absolutely nothing
var express = require('express');
var app = express();
var serv = require('https://'example'.com/MyID').Server(app);
app.get('/',function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(2000);
console.log("Server started.");
Please help me I do not know what to do.
Related
hyper terminal
my local server
code is :
const express = require('express');
const app = express();
const port = 3000;
app.use("/",(request ,res)=> res.end("Hello World"));
app.listen(port , ()=>{
console.log("server is at 3000");
});
Looks like you miss typed res.send
app.use("/", (request ,res) => res.send("Hello World"));
I am using the following simple server.js to randomly point to two different HTML files on the server. However, it is automatically redirecting to index.html (not even in the parameters any more) and not the index1.html or index2.html.
I am not sure what I am missing here
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
app.use(express.static('public'))
app.get('/', function(req, res) {
if((Math.floor(Math.random() * 2) + 1)>1)
{
res.sendFile(__dirname + "/public/index1.html");
}
res.sendFile(__dirname + "/public/index2.html");
});
/*--------------------Routing Over----------------------------*/
app.listen(port, function () {
console.log(`Server listening on port ${port}!`);
});
As i executed your code it was looking fine to me and changing file index1.html and index2.html randomly.
If you want to change with route as well then i'll suggest below scenerio :
var express = require('express');
var app = express();
var port = process.env.PORT || 3002;
app.use(express.static('public'))
app.get('/index1.html', function(req, res) {
res.sendFile(__dirname + "/public/index1.html");
});
app.get('/index2.html', function(req, res) {
res.sendFile(__dirname + "/public/index2.html");
});
app.get('/', function(req, res) {
if((Math.floor(Math.random() * 2) + 1)>1)
{
console.log("index1");
res.redirect("/index1.html");
}
console.log("index2");
res.redirect("/index2.html");
});
app.listen(3002);
To send either index1.html or index2.html you have to use the else condition. Further, I have used the path module to create the path, which is the best practice.
var express = require('express');
var app = express();
var path = require("path");
var port = process.env.PORT || 3000;
app.get('/', function (req, res) {
if ((Math.floor(Math.random() * 2) + 1) > 1) {
res.sendFile(path.join(__dirname, "/public/index1.html"));
} else {
res.sendFile(path.join(__dirname, "/public/index2.html"));
}
});
/*--------------------Routing Over----------------------------*/
app.listen(port, function () {
console.log(`Server listening on port ${port}!`);
});
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 run my application is written in Node.js using CMD (Windows OS).
node server.js
There is console.log('Client connected...'); in server file.
But I dont see logs in CMD whene I application is working. How can I fix it?
My code is:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.use(express.static(__dirname + '/bower_components'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
server.listen(4200);
io.on('connection', function(client) {
console.log('Client connected...');
});
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);
});