Unexpected error using Node.js's Express with vhosts - node.js

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;

Related

React not recognizing Express

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

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.

nodejs chatroom object expected line one

I'm using this tutorial to try to learn how to make a chatroom with nodejs.
I got down to where it shows the page with a button and text area, but then when I ran it, it gave an object expected microsoft javascript error on line one, which remained the same as before.
var express = require("express");
And while i'm here I might as well ask... is the following code:
var express = require("express")
, app = express()
, http = require("http").createServer(app);
the same as this code:
var express = require("express");
app = express();
http = require("http").createServer(app);
Node.JS won't give you a Microsoft JavaScript error. I'm guessing you're trying to run your script under Windows Script Host, which won't work. You need to download Node.JS and run it under that.
From your comment, you note that you're running your script from the Windows Command Prompt:
D:\> chat.js
That will open it with the default document viewer, which just happens to be Windows Script Host here. To run it with Node.JS, you need to explicitly add node:
D:\> node chat.js
As for the second question, no, but almost. You can declare multiple variables in one line, e.g.:
var a, b, c;
You can also initialize them all:
var a = 1, b = 2, c = 3;
This is not the same thing as
var a = 1;
b = 2;
c = 3;
Using commas makes them part of the var statement and create a new variable. Without the var, it will use a previously-declared variable or, worse, put it in the global scope.

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.

ERR_SSL_PROTOCOL_ERROR browser error message while running a https server in nodejs express

I have a number of nodejs applications running on Express using the code below. They all run fine using code similar to the following:
fs = require 'fs'
https = require 'https'
express = require 'express'
server_port = 3000
keys_dir = 'keys/' server_options = {
key : fs.readFileSync(keys_dir + 'privatekey.pem'),
cert : fs.readFileSync(keys_dir + 'certificate.pem') } app = express.createServer(server_options)
app.listen server_port
console.log "HTTPS Server started on port #{server_port}"
However, when trying to create a new application using this code I see a ERR_SSL_PROTOCOL_ERROR when starting the https server. Any idea what is causing this problem?
I discovered that was caused when moving from express 2.5.8 to express 3 - specifically 3.0.0beta4. When creating a new project the version pulled from npm had changed to the version 3 series. Even though the module is marked as "beta" when you run express --version this version is what is installed now when running npm install express. The details of the changes are outlined here.
To solve this for my case I used the following code:
const fs = require("fs");
const https = require("https");
const express = require("express");
const keysDir = "keys/";
const options = {
key : fs.readFileSync(keysDir + "privatekey.pem"),
ca : fs.readFileSync(keysDir + "certrequest.csr"),
cert : fs.readFileSync(keysDir + "certificate.pem")
};
const app = express();
https.createServer(options, app).listen(3000);

Resources