nodejs chatroom object expected line one - node.js

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.

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

Application-scope Variables in Node.js?

Is there a variable scope in Node.js that persists longer than the request, that is available to all application users? I'd like to create a variable that can be read/written to by multiple app users, like so:
var appScope.appHitCount += 1;
The session scope won't work because it is user specific. Looking for application specific. I couldn't find anything in the Node.js docs. Thanks.
If you have a variable scoped to the app module it will be available and shared by everything within the scope. I imagine in a big app you would need to be careful with this, but it's just regular javascript scoping. For example this will continue to append to the array as you hit it:
const express = require('express')
const app = express()
var input = []
app.get('/:input/', (req, res) => {
var params = req.params
input.push(params.input)
res.send("last input was: " + JSON.stringify(input))
})
app.listen(8080, () => console.log('Example app listening on port 8080!'))
Now visiting 'http://localhost:8080/hi' returns:
last input was: ["hi"]
and then 'http://localhost:8080/there' returns:
last input was: ["hi", "there"]
...etc.
If you want something shared by the entire app (i.e. all the modules) you could set up a module that is require()d by all the modules and i=has the responsibility of getting and setting that value.

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

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;

Resources