React not recognizing Express - node.js

In my jsx file, I have the following:
var express = require('express');
var myExpress = express();
var http = require('http');
var app = http.createServer(myExpress);
var { Server } = require("socket.io");
var myio = new Server(app);
However, the browser says "Uncaught TypeError: express is not a function"
I have tried importing express with an import statement, as well as making my project a module in my package.json. What is weird is that when I use the same code in a regular js file in the same folder, it works perfectly well. This code was the code in every single one of the tutorials, so I am at a loss. Thank you.

Express is node framework you can't use it in react.
I think you need https://v5.reactrouter.com/web/guides/quick-start

Related

What is the second pair of parenthesis in "require('express')()" in node.js?

I'm following the socket.io chat application tutorial here - https://socket.io/get-started/chat/
and it's using express framework. There's a code in the tutorial that is:
var app = require('express')();
var http = require('http').Server(app);
and if I just use 'require('express')' without the second parenthesis it doesn't work. What does the second parenthesis do and where could I find documentation for the syntax?
The type returned from require('express') is a function. The second set of parenthesis is you actually invoking the function to create an instance of an express app.
It's the equivalent of you doing
const express = require('express');
const app = express();
The require('express') call returns a function and hence you need to call that returned function in order to initialisation an express app. For example, the main entry file of express might look like
function one(){/*some code here*/}
function two(){/* some other code here*/ }
Module.exports = one;
So essentially what happens is that when you require express the above code is included and the one function is exported for you to use and hence you need to call that function that's why you have that extra parenthesis

Understrap 'require is not defined'

I'm using the Understrap theme for my Wordpress project. I created a simple custom .js file to add to my project but I'm getting a 'require is not defined' error.
custom .js file
var express = require('express');
var moment = require('moment');
var app = express();
var m = moment();
app.get('/', function (req, res) {
m.set({'year': 2017, 'month': 5, 'day': 29});
//res.send(moment().format('YYYY/M/D'));
var today = document.getElementById('today');
today.innerHTML = moment().format('YYYY/M/D');
})
Nodejs is a server side language, you cannot use require in browser side unless you use browserify or webpack. And also you cannot use express in browser because it's a server side framework, as you are using a theme for wordpress, I don't think you need to use nodejs at all, just write the plain js.

Instantiating express

I have an issue where I need to load express like this:
var express = require('express');
var app = express();
In order to get .static to work:
app.use(express.static(__dirname+'views'));
Any reason why I can't use shorthand:
var app = require('express')();
When I try the short hand it says express.static is undefined and my script won't run. Is this just a feature that's not supported by express?
Any reason why I can't use shorthand:
var app = require('express')();
If you considered this statement from your script,
app.use(express.static(__dirname+'views'));
you are using static method of express.In order to use this method,you must import express first and store it in some variables like u did
var express = require('express');
From express#express.js
exports.static = require('serve-static');
static defined at class level.
then instantiate it
like this
var app = express();
to get the access to object level(prototype) method and properties like
app#use app#engine etc.
From express#application //line no 78
EDIT :
but then why can't I use app.static if I did var app = require('express')();
As I said,.static is the class level method and not the instance/object(prototype) level.
So,by var app = require('express')()
you will get express instance / object (prototype) which dont have app.static method.So,you can't use.
Read more javascript-class-method-vs-class-prototype-method
This will work: const app = (() => require('express'))()();
But you still need express itself, so there literally is no real point to requiring twice.

Unexpected error using Node.js's Express with vhosts

Here's the error:
Here's the code:
runapps.js:
var express = require('express');
var app = express();
var first = require('./apps/ghost_gforce/index.js').app;
var second = require('./apps/ghost_blog2/index.js').app;
app
.use(express.vhost('gforce/main', first))
.use(express.vhost('gforce/blog2', second))
.listen(80);
running this straight from node like below works perfectly:
node apps\ghost_gforce\index.js
UPDATE:: ran:
npm install
within the node app directory, so nothing should be missing.
Thanks in advance!
In your test you are calling a relative path:
apps\ghost_gforce\index.js
But in your code you are using a path based on the root of your filesystem:
/apps/ghost_blog2/index.js
Try this:
var first = require('./apps/ghost_gforce/index.js').app;
var second = require('./apps/ghost_blog2/index.js').app;

unable to execute server.js program using express framework on node.js

While trying to execute server.js program I am getting the following error:
var app = express();
Type error: object is not a function
at object.<anonymous>
even tried re installing and changing the version of express to
npm install
npm uninstall express
npm install express#2.5.9
but it resulted in new error
fqdn = ~req.url.indexof(' ://')
I use windows and i am working on node.js version 0.8.4.
If you're using Express < 3.0, the return value of require('express'); is not a function; you'll need to create a server the old way.
Express 2.x
var express = require('express');
var server = express.createServer();
Express 3.x
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
What does
> require('express').version;
'3.0.0rc2'
return?
As you can see it does return 3.0.0rc2? Does yours really return 2.5.9. if it does you use like Brandon said 2.x section. If it returns 3.x you use his 3.x section.

Resources