Error with middleware and NodeJS - node.js

I have this :
app.use(session({secret: 'JKSBDFJKQS444SQ4DQSND'}));
But when I run my app file I got this error :
Error: Most middleware is no longer bundled with Express and must be installed separately.
How to fix ?

From the express 4.x Docs "As of 4.x, Express no longer depends on Connect. All of Express' previously included middleware are now in separate repos. Please view the list of middleware. The only included middleware is now express.static()." See here for the details.

Related

Remove warning on Node.js WebStorm

I am learning backend with Node.js and using middleware to catch for errors and send a different response code and body.
The warning is similar to err.message.
How do I remove these warnings?
Are you using express? Its methods are generated dynamically in runtime, so they can't be resolved during static code analysis. Installing TypeScript stubs should help to get methods work: put cursor on 'express' in const express = require('express'); , hit Alt+Enter and choose Install TypeScript definitions for better type information to install typings - see https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html#ws_jsconfigure_libraries_ts_definition_files

Serving static files in Express (express.static vs app.static)

Through serving static files in Express .. I saw below code:
const express = require('express');
const app = express();
// Initialize the main project folder
app.use(express.static('website'));
Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?
Note: I tried to replace express with app and it said app.static is not a function. I also saw some NPM packages that use app.static() like wamjs for example, which is weird.
app.static() has nothing to do with Express.
Wam is a completely different framework (that may be Express-like in some ways, but it's not Express and not identical to Express). Here's a description on the NPM wam.js page:
Wam is a small koa and next.js inspired middleware framework for node.
If you want to program with Express, then use the Express documentation, not the Wam documentation and it will guide you to use app.use(somePath, express.static()). You can see in the Express doc for the app object, there is no mention of app.static(). That is apparently something that wasm.js invented for it's own framework.
Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?
Because Express doesn't have app.static(). It has express.static().
I also saw some NPM packages that use app.static() like wamjs for example, which is weird.
I wouldn't call it weird. wamjs is a different package with a different API. It is not Express so there should be no expectation that Express behaves like wamjs or that wamjs behaves like Express. They are different frameworks.

Connect-mongo Alternative for Express 4

I am looking to implement cookiestore in my Express app, I followed this question
Best Session Storage Middleware for Express + MongoDB
and
https://github.com/kcbanner/connect-mongo
for my Express 3.x project, but for Express 4, connect middleware is deprecated.
Which is the suitable alternative for connect-mongo?
Middleware has been extracted out of the core and moved individual modules. This changes how you set up the app but you have the option to use the same middleware as before. The overview explaining how to migrate from 3.x to 4.x lists the modules that can be used as replacements for the Connect middleware.
The syntax will be slightly different as you explicitly install the modules, such as express-session, and the app.use statements are modified to reflect this. The options you pass to these modules, however, are the same as it was with the Connect middleware...so you can pass your connect-mongo details to express-session and keep rolling along.
So you don't have to change unless there's another problem that isn't clear in your original question...and there could be other problems if you have a large, established app. But if you are following a tutorial, you should be early enough in the process that this won't be a major issue.
Edit: It looks like there's also been discussion about Express 4 on the connect-mongo github page. There are more examples there about how to use this module with the new version of Express...
Edit 2: The code, referenced a few times on the github page, looks like this:
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret: 'youshouldputyourownsecrethere',
store: new MongoStore({
db : mongoose.connection.db,
})
}));

Express 4.x with Typescript

I am trying to make a express app with typescript.
This is my code so far:
//<reference path="./server/types/node.d.ts"/>
//<reference path="./server/types/express.d.ts"/>
import express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('hi');
});
app.listen(3000);
nothing really shocking, I am just trying to make this work, but somehow, always when I try translate this file to a js file. I get strange errors, even if I change the express version to 3.1 (the express.d.ts is just supported for express 3.1 not for 4.x)
Any idea, where I can get a express.d.ts file for express 4.x or what I am doing wrong?
>> error TS2071: Unable to resolve external module ''express''
>> error TS2071: Module cannot be aliased to a non-module type.
>> error TS2095: Could not find symbol 'express'.
You reference comments are wrong. There needs to be three slashes /// :
///<reference path="./server/types/node.d.ts"/>
///<reference path="./server/types/express.d.ts"/>
The only way you can get that error if you are using this reference file https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express.d.ts#L26 was that your reference comments were wrong and typescript was not reading that express.d.ts :)
Might I add that because you are using express 4.x and the type definitions are not up-to-date, you will be missing some key features that are central to express 4.x, such as Routers.
You have two choices at this point: update the type definitions, or use
var express = require('express');
instead, which removes some of the benefits that typescript provides, but is something you're bound to run into in the future with other node modules, such as mongoose.

Node - Express 4 csrf

With the dawn of Express 4 connect is no longer a dependancy. I can't find on the express site any new implementation of csrf.
the old way was app.use(express.csrf()); and then add in a middleware function to expose it in the view.
My question is: Am I now forced to include connect as a dependency, or is there something baked into express now that allows for csrf that I am missing?
Or can I just include the source of csrf into my app?
You can install and require csurf for Express 4.

Resources