Ok so my question is concerning the websockets api...NOT socket.io. npm install websocket
Here's my problem.
I have written a little websockets server that can receive a message, interpret the textual message and send it back with a little extra text. Simple enough.
The message comes from an index.html file with a javascript...script written in it. Everything works as expected. The node server runs locally on my machine and when I want to test it out I just start the server then double click the index.html file which runs the script, which starts a new connection with node and done.
What I want is to be able to navigate my browser to "localhost" and the server start by giving the client the index.html file, which in turn will establish a connection with the server.
How can the user get the index.html file by simply navigating to a url (localhost in my case), if a connection is required to get the index.html file. In Socket.io it looks like this:
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {});
If my question is still unclear here is a rewording: A user needs to be able to go to my url (localhost since node is running locally) and receive index.html. Like, all requests to the root receive an index.html file within a public directory.
The module you are using includes a complete example of how to use it with Express. It looks like it's written for the Express 2 API, so you have to tweak it if you're using Express 3:
var app = require('express')()
, http = require('http')
, server = http.createServer(app)
, WebSocketServer = require('websocket').server;
var wsServer = new WebSocketServer({
httpServer: server
});
server.listen(80);
Related
I created a small express server to serve my react app. It's a static context react app that only uses API calls to fetch and show data.
When I start my server everything goes well and when I navigate to localhost:5000 the server automatically redirects to localhost:5000/login. However, the server throws an error once the login page is loaded and I trigger a reload. It then gives me Error: Cannot GET /login.
Does anyone has an idea what goes wrong? Here is the code of my express server script which I trigger with yarn start (added start script in package.json ("start": "NODE_ENVIRONMENT=production node server.js").
const express = require("express");
const app = express(); // create express app
// add middleware
app.use(express.static("dist"));
// start express server on port 5000
app.listen(5000, () => {
console.log("server started on port 5000");
});
I am new to nodeJS server area, need help in understanding how to work with REST API (using express) and deploy the angular application over a singe node server and same ports.
By deploying i want to understand if user hit below url http://localhost:8000/<page_name> then the specified page should open.
And is user hit below url using get or post request
http://localhost:8000/api/<api_name> then a json or a text will be returned.
How to run both the thing over a single node server.
Lets assume, you have all your static files in the /public folder of you app. Generally spoken, if you are using express.static, you should also get your index.html because this is handled by default for each directory.
In your case, as you are using Angular, the routing is handled from the client side (SPA). You should only have one single index.html after building your Angular app. All files from your dist folder should then be placed into your /public folder. Then you need to make sure, that initial file serving provides your index.html like so:
In this example static files are served first, then your API and if nothing is found, you are getting back you index file.
const express = require('express');
const app = express();
// serve static files
app.static(__dirname + '/public'));
// serve your API
app.get('/api/welcome', function (req, res) {
res.send('Welcome');
});
// fallback routing (server side handling)
app.get(/.*/, function (req, res) {
res.sendFile(__dirname + ‘/public/index.html‘
});
app.listen(3000);
Next time please make sure, to give all necessary information in your question ;-)
With the help from Sebastian, so far I can find a solution but its not working when i am hitting URL for different pages.
const express = require('express');
const app = express();
app.use(express.static('public'))
Please provide your suggestions.
I want to serve html to the browser but for now just 'Hello World'
When I navigate to https://example.com/test
EDIT: my site is not example.com I just can't type the name
the console does not log a request
404 in the browser if I don't create the directory, 403 forbidden if I do create it.
node.js code:
var express=require(__dirname+'/../node_modules/express');
var app = express()
app.get('/test', function(req,res){
console.log(req);
res.send('Hello World')
})
app.listen(10001);
I get no node.js errors
the port 10001 is open!
I can't find any docs beyond http://expressjs.com/en/guide/routing.html which just assumes that what they tell you to do will just work so they go into ZERO detail!
I totally 100% do not understand this!
Where are you hosting? If local, are you including the port in your URL?
If you are using a local dev server, it should be something like
https://localhost:10001/test
You are trying to serve your app on a server at port 10001. If your domain is example.com, you have to go to http://example.com:10001/test. When you try to connect to the website using a browser, the browser will automatically assume the port is 80 and it will try to connect to that port.
Also you can use const express = require("express").
So this is more of an open ended question: I've started working with node and I've been creating command line applications for practice. The majority of these apps take command line arguments and make http requests to an API and serve up the results based on the arguments passed. The thing is, I would like these programs to have useful front-end interfaces so that the results are not just display via the command line terminal. Is there an easy way to accomplish this? Is this what Express is useful for?
perhaps more fully, that's what express is for and that's what routes do for you - so that your browser can be directed to a default (e.g. index.html) page or a specific page or service. If you're rendering basic html pages, stored in an /HTML folder, to the user, then you might have the following kind of code in your app:
var express = require('express');
var app = express();
app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/HTML'));
followed by a series of app.get('path/from/browser') and/or app.post('path/from/broswer') statements which tell your nodejs server what to do when various get and post commands are sent to the app.
as your app gets more complex, you may want to consider the router service as a way to structure your application code and associated services.
you also need to start an http server, so the browser can actually talk to the server. You would do that in a very simple way by executing the following code:
var cfenv = require('cfenv');
var appEnv = cfenv.getAppEnv();
app.set('port', appEnv.port);
var server = app.listen(app.get('port'), function() {console.log('Listening on port %d', server.address().port);});
In this simple example, your app is now using 3 new services: express, ejs, and cfenv. You would use the standard npm install process to get this into your local app so that you can use them. From your application root folder, you would execute npm install --save express, repeating for each of the three new services.
I just set up a NodeJS server and wanted to use the vhost function from Express to allow for easy project setup. I want to be able to create a new directory for a new project without going through the hassle of creating new subdomains etc.
Basically, I want to be able to reach project1 at node.domain.com/project1 and project2 at node.domain.com/project2.
I'm now running server.js, which is located in the root (node.domain.com/server.js) and contains:
var express = require('express');
var app = express();
app
.use(express.vhost('node.domain.com', require('./project1/app.js').app))
.use(express.vhost('node.domain.com', require('./project2/app.js').app))
.listen(3000);
It's all working fine, when I go to node.domain.com/project1 I get the results of ./project1/app.js but whenever I change something in any of the app.js files it requires a restart (Ctrl+C followed by node server.js) for it to update the changes.
And the contents of app.js are, for example:
var express = require('express');
var app = exports.app = express();
app.get('/project1', function(req, res){
res.send('Hello World [/project1]');
});
Any idea why this is?
A thing to note is that I also run Apache on the same server, and I'm using this guide to allow both servers to run on port 80 (but accesible via different subdomains).
Oh! I feel dumb now, seems like I need to use something like forever, with its w flag.