I am trying to deploy my ext js app in node server. Steps i have followed.
1.Created a Extjs app using sencha cmd and had used sencha app build to build my app
Once after building successfully i have taken my app in build-->production folder to my node server folder.
below screenshot contains dbview(client) files
When i start my node server and run my applicaiton using http://localhost:3000 getting following error in my console
Please find my server code
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.sendFile(__dirname+"\\dbview\\index.html");
})
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
Help me with the problem
You need to mount the directory so that express knows to look for static content there, or you could go the long way about it and create a specific route handler for that file:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname+"\\dbview\\index.html");
});
// THIS IS WHAT YOU NEED
app.use(express.static(__dirname));
// OR THE LONG WAY
app.get('/app.json', function(req, res) {
var options = {
root: __dirname,
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
res.set('Content-Type', 'application/json');
res.sendFile('app.json', options);
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
See the Express Documentation for further details.
Related
This is my first topic, and I'm not really that great with js, but how do I get my index.html file to show up? This is what I have.
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000
app.get("/", function (req, res) {
res.send("hello!")
});
app.listen(PORT, function () {
console.log("Server is running on localhost:" + PORT);
});
app.js and index.html are in the same directory. Thanks!
You can try using the following code into app.get function
res.sendFile(__dirname+/index.html");
I'm new to node.js and socket.io. They asked me to divide the connect4 game (I made with Javascript,node.js, express, socket.io, jquery ) into 2 separate apps (server app and client app).
The node.js of these two Apps must be connected to each other. In other words, I need to connect Client App's Node application to the port of the Server App's node application. (They said I should have 2 working ports belonging to 2 applications)
(Before divided the app it was working normal; when client give the url of the room he can play with his friend, game start and over normally But now everything mixed.)
Connect 4 game was working when client and server in the same App but now I cant do something like that
"
app.get('/:room([A-Za-z0-9]{9})', function(req, res) //app.set('view engine', 'html'); //res.status(200).sendFile(__dirname+'./index.html');
"
But my main problem is I couldn't connect from Client App nodejs to Server app nodejs .
Code part of the server app;
const express = require('express');
const cors = require('cors');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http ,{
cors: {
origin: '*',
methods: ['GET', 'POST'],
},
serveClient: false,
pingInterval: 5000,
});
const port = process.env.PORT || 4000;
const { randomNumeric } = require('./utils.js');//I want random room key for later
// TODO: IF CORS DOES NOT WORK, TRY TO USE IT IN EXPRESS SERVER
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST');
res.header('Access-Control-Allow-HEADERS', 'Content-Type');
});
io.listen(port, () => { //Its not working :(
console.log("çalışıyor");
console.log(`Server started at ${port}`);
});
And Client App node.js code;
var express = require('express'),
game_logic = require('./game_logic'),
app = express(),
server = require('http').createServer(app),
port = Number(process.env.PORT || 3000);
server.listen(port);
console.log('Node.js web server port 3000 de çalıştı')
/*routing*/
app.use("/css", express.static(__dirname + '/css'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/img", express.static(__dirname + '/img'));
app.get('/', function(req, res) {
res.sendFile(__dirname+'/index.html');
});
And in Client App I have App.js for socket.io connect
$(function(){
var socket = io.connect(),
player = {},
yc = $('.your_color'),
oc = $('.opponent_color'),
your_turn = false,
url = window.location.href.split('/'),
room = url[url.length-1];
after that I have code for connect socket.io like socket.on and socket.emit
when I run this 2 App and open the local3000 I just saw empty game board nothing happened,
Can you help me about this and I really need to some links about this topic. Thank u
Working with express Router for getting for the first time.
This is my route.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('home page');
});
module.exports = router;
This is my index.js
var express = require('express');
var app = express();
var router=require('./route.js');
app.use('/route',router);
var server = app.listen(process.env.PORT ||8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port)
});
When I open run it in browser it shows:
Cannot GET /
The only URL the code you have written will respond to is:
www.example.com/route/
If you want it to respond to:
www.example.com
then change to the following in your index.js file:
app.use('/', router);
You should replace
app.use('/route',router); with app.use('/', router);
As I may see you have created default route in app.use as /route
you must not have added that using app.use('/') would be enough instead of creating another route for that
Thanks.
I want to deploy a server running on NodeJS to my cloud server.
The server:
OS: CentOS 6.5
External IP: 123.125.130.102
NodeJS code:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("http://%s:%s", host, port)
});
I am running an instance on this server.
NGINX config:
server {
listen 80;
server_name 123.125.130.102;
location / {
proxy_pass http://localhost:8081/;
}
}
But I can't seem to access my node application on http://123.125.130.102:80.
I'm new to NodeJS and I don't what is wrong.
I'm a beginner programmer and pretty new to Node.js.
I managed to setup a single static page by using AWS EC2 and Heroku, but I need help making other subpages. ie. mysite/blog or mysite/archive.
I started out with a simple web.js file I got from a sample node app which was:
var express = require('express');
var app = express();
app.use(express.logger());
app.get('/', function(request, response) {
response.send('Hello World!');
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
All that said was Hello World so I created index.html and changed web.js to this.
var express = require('express');
var fs = require('fs');
var htmlfile = "index.html";
var app = express(express.logger());
app.get('/', function(request, response) {
var html = fs.readFileSync(htmlfile).toString();
response.send(html);
});
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log("Listening on " + port);
Now that serves index.html instead, but how do I get /blog or /archive to work?
You can add other routes to your code to handle specific URL formats. Make sure the more specific URLs are listed before the general route (/ or *)
// Handle request for blog
app.get('/blog', function(req, res){
res.send('A list of blog posts should go here');
});
// Handle request for archive
app.get('/archive', function(req, res){
res.send('Archive content should go here');
});
// Route for everything else.
app.get('*', function(req, res){
res.send('Hello World');
});
I've got a more lengthy explanation about this here: http://hectorcorrea.com/#/blog/introduction-to-node-js/51