react and node.js project with express - javascript

I just got an existing project with react and node.js
When I run the react app the server call fails,
the React is on 8080,
the server looks something like this
// Listening on port 3000 for testing and 80 for prodution
app.listen(3000, function () {
winston.log('info', 'Listening on port 3000!');
});
// Index is currently inactive
app.get('/', function (req, res) {
res.send('');
});
// Call and load React
app.get('/app', function(req, res) {
res.sendFile(path.resolve(__dirname, 'public/index.html'));
});
// API for Web App
app.use('/api/v1', routes)
// API for Mobile App
app.use('/mapi/v1', mroutes)
// DEFINE ERROR BEHAVIOUR
// =============================================================================
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status = 404;
res.sendFile(path.resolve(__dirname, 'public/index.html'));
// next(err);
});
this is how I got the project, I'm assuming it should work, I just need to make the node project somehow to run on port 8080(with the react app)
.
the client request is to localhost/8080(but changes depeneding on the port of the project),
if I'm runing react on 8081 the error will be "can't find localhost 8081/api/v1/GetUsers(example..)"
the means that the previous developer intended that the server and the client will be on the same port, but I don't how

Related

When i use express and use ''use'' request to get response this came in my chorme bowser Cannot GET

iam new in node.js .iam faceing this problem .When i use express and use ''use'' request to get response to the chorome but this erro came in my chorme bowser Cannot GET.
or my app.js file or index.js is in one same folder . folder name is static. Iam not good in english please help me. Or if i use nodemon togther than when i get to localhost the the loaclhost cannot and show this 'This site can’t be reached'
i dont know how to fix it can any help me to fix this problem this the code
const express = require("express");
const app = express();
const port = 8080;
app.use('/static',express.static('express/static'));
app.get("/", (req, res)=>{
res.status(200).send("This is the first page of node express");
});
app.get("/about", (req, res)=>{
res.send("This is the about page of node express");
});
app.post("/postabout", (req, res)=>{
res.send("This is the postabout page of node express");
});
app.post("/erro", (req, res)=>{
res.status(404).send("This page has been errored in code of 404");
});
app.listen(80,'127.0.0.1', () =>{
console.log(`The application started and the port is ${port}`);
});
This the image of my code and chrome
In app.listen you passed wrong port means you intialized port as 8080 but passed 80
app.listen(8080,() =>{
console.log(`The application started and the port is ${port}`);
});
Basically, app.use function uses a middleware when a particular API is called it does not a get request.
So try app.get instead of app.use it will work.
app.get('/static', (req, res) => {
// Do your stuffs.
return res.send(response)
})

Function execution took 60060 ms, finished with status: 'timeout'. Google cloud functions

I am trying to upload my nodejs work on googlecloud functions but i am getting timeout error in logs. I am trying to load the homepage using templates. I am unable to use the helloworld function properly. Below is a part of the code and not a complete code.
exports.helloworld = ()=>{
app.get('/',(req, res)=> {
res.render('home');
});
}
app.listen(3000, () => {
console.log('app now listening for requests on port 3000');
});
If you want to use express js routing in the cloud functions, you need to export the express js app instead of running the server.
index.js
const express = require('express');
const app = express();
app.get('/',(req, res)=> {
res.render('home');
});
app.get('/login',(req, res)=> {
res.render('login');
});
exports.helloworld = app;

Express routing does not work with simple test case

I think I am missing some concept with basic routing for Express. See here
I created some simple test code as follows in my server index.js file.
app.get('/foo', function (req, res) {
console.log('foo path found');
res.send('foo achieved')
})
In my browser(chrome) URL I type
localhost:3000/foo
to trigger the route but I get no response on the server or client.
I verified localhost:3000 is up and running.
Port is set in a different file as follows:
app.set('port', (process.env.PORT || 3000));
But also I get confirmation in the terminal as follows:
const server = app.listen(app.get('port'), () => {
console.log('DEBUG: express: server up');
});
I am on a campus network that blocks some traffic, but b.c. this is localhost I don't think it should matter.
I don't think you're supplying enough information to correctly debug your issue.
I'd initially ensure that Express is listening on port 3000, double-check this line:
app.listen(3000);
Ideally, this line should be at the bottom of the script.
Response to edit: Yes, this should not matter. localhost is an alias for the system itself. It's a loopback, similar to that of 127.0.0.1.
It seems like you have created two express app, as you have mentioned that you are using two different files, the localhost which you are able to run is the one which has app.listen() code, but this doesn't have the app.get()
I suggest you use a single file for now and try doing it.
Try out the following code, and check now with localhost:3000/foo.
const express = require('express')
const app = express()
const port = 3000
app.get('/foo', function (req, res) {
console.log('foo path found');
res.send('foo achieved')
})
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

Electron Auth0Lock "Origin file:// not allowed"

Trying to get auth0 working with my electron app. When I follow the default tutorial and try to authenticate with Username-Password-Authentication, the lock fails with a 403 error and responds with "Origin file:// is not allowed".
I've also added "file://*" to the Allowed Origins (CORS) section of my client settings in the auth0 dashboard.
Auth0 Lock with console errors
Origin file:// is not allowed
EDIT:
Lock setup in electron
var lock = new Auth0Lock(
'McQ0ls5GmkJRC1slHwNQ0585MJknnK0L',
'lpsd.auth0.com', {
auth: {
redirect: false,
sso: false
}
});
document.getElementById('pill_login').addEventListener('click', function (e) {
e.preventDefault();
lock.show();
})
I was able to get Auth0 to work by using an internal express server in my electron app to handle serving pages.
First I created a basic express app in a separate folder in my project called http, here will be the express server code and html files to serve.
const path = require('path');
const express = require('express');
const app = express();
app.use(express.static(process.env.P_DIR)); // Serve static files from the Parent Directory (Passed when child proccess is spawned).
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:<PORT>'); // Set this header to allow redirection from localhost to auth0
next();
})
// Default page to serve electron app
app.get('/index', (req, res) => {
res.sendFile(__dirname + '/index.html');
})
// Callback for Auth0
app.get('/auth/callback', (req, res) => {
res.redirect('/index');
})
// Listen on some port
app.listen(<SOME_PORT>, (err) => {
if (err) console.log(err);
console.log('HTTP Server running on ...');
});
Then in the Electron main process, I spawn the express server as a child process
const {spawn} = require('child_process');
const http = spawn('node', ['./dist/http/page-server.js'], {
env: {
P_DIR: __dirname // Pass the current dir to the child process as an env variable, this is for serving static files in the project
}
});
// Log standard output
http.stdout.on('data', (data) => {
console.log(data.toString());
})
// Log errors
http.stderr.on('data', (data) => {
console.log(data.toString());
})
Now the auth0 lock authenticates as expected.

Integrate Express.js-REST-Endpoint with Meteor Application

I have a kinda tricky situation: I'm currently building a full meteor-featured application. But I also need to expose some functionality as REST-Service for automation reasons (a third party application should be able to insert and receive data via REST).
The express.js-package seems to be a very solid option for building a REST-Endpoint into a node.js environement but I'm wondering how to integrate this endpoint into meteor.
What I want is to access the "normal" Site via for example http://myfancysite.com/my-display-route and at the same time be able to access my REST-Endpoint via for example http://myfancysite.com/api/insert-crazy-data/.
The "normal" Site is accessible via the port defined when starting Meteor. The thing is, that I have to specify a different port for express.js to listen on and I want both - meteor and express - to share the same port since I don't want to access the REST-Endpoint on a different port.
Is this somehow possible? :D
Here's some code I use for express at the moment.
//<meteor-root>\server\main.jsx
import { Meteor } from 'meteor/meteor';
// do some meteor things
...
//require express
var express = require('express');
//create application
var app = express();
//use environement defined port or 3000
var port = process.env.PORT || 3000;
//create router
var router = express.Router();
//define routes
...
//register all routes with '/api'
app.use('/api', router);
//start server
app.listen(port); // <= this should be the same port as the meteor application itself!
console.log('listening on port ' + port);
Meteor is essentially a node app that already exposes a connect http server, which means you can define server routes simply like:
import { WebApp } from 'meteor/webapp';
WebApp.connectHandlers.use('/hello', (req, res, next) => {
res.writeHead(200);
res.end('Hello world from your server');
});
If you insist on using express, then you can register your express routes as connect middleware like so:
import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import express from 'express';
const app = express();
app.get('/api', (req, res) => {
res.status(200).json({ message: 'Hello from Express!!!'});
});
WebApp.connectHandlers.use(app);
Et voilà!

Categories

Resources