Property 'register' does not exist on type 'NestExpressApplication' - nestjs

According to nestjs documentation I want to use fastify-helmet in order to config contentSecurityPolicy.
first I installed the following package:
$ npm i --save fastify-helmet
in my main.ts I registered fastifyHelmet but it throw the following error :
Property 'register' does not exist on type 'NestExpressApplication'
async function bootstrap() {
const logger = new Logger('bootstrap')
const app = await NestFactory.create<NestExpressApplication>(AppModule);
await app.register(fastifyHelmet); // this line thorw error
...
const PORT = process.env.PORT
await app.listen(PORT);
logger.log(`Application is start on port : ${PORT}`)
}
bootstrap();

The problem is that you are trying to register a fastify package to your express based app which does not work.
There are 2 possibilities to fix your issue:
Use a express compatible package (for example helmet: npm i --save helmet) and then register it with: app.use(helmet) (express does not use .register but .use)
Change your NestJS App to Fastify: (npm i --save #nestjs/platform-fastify) and then: const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter()); -> then the fastify-helmet package (and .register) should work!
More details about using fastify instead of express are available at the official documentation page: NestJS Documentation

Related

How to use Bolt events with the newer Slack API manifests?

I'm building a Slack App using Bolt and I've got the basics working using Socket Mode. The docs say that socket mode apps are not allowed in the public directory, which I do want my App in when it's ready. I've now turned off socket mode and got ngrok working as described here. Slack was able to validate the url anyway.
But what's not working is a slash command. The manifest editor says the url is required for a slash command, but how does that line up with bolt? Are there better docs for non-socket-mode somewhere? It seems like every example of using bolt says "let's use socket mode, it's easy".
Manifest portion:
slash_commands:
- command: /sb
url: https://[my url].ngrok.io/slack/command
Sample code:
const { App } = require('#slack/bolt');
const express = require('express');
const app = express();
const boltApp = new App({
signingSecret: config.slackApp.signingSecret,
token: config.slackApp.token,
endpoints = '/'
});
app.use('/slack/events', boltApp.receiver.router);
Bolt
Slack App Manifests
I got this working with a combination of the following:
setting every url in the manifest (slash_commands, event_subscriptions, interactivity) to https://foo.ngrok.io/slack/
attaching Bolt to an existing Express App, attempting to follow this PR to use app and/or router config prop on ExpressReceiver, but strangely what worked was putting the express app into the router
setting up Bolt like below
Example Code:
const expressApp = express();
...
const boltReceiver = new ExpressReceiver({
router: expressApp,
signingSecret: SLACK_SIGNING_SECRET,
endpoints: '/slack'
});
const boltApp = new App({
token: SLACK_BOT_TOKEN,
receiver: boltReceiver,
appToken: SLACK_APP_TOKEN,
socketMode: false,
});

Problem using a npm package in React - says "TypeError: require is not a function"

I'm trying to use the package lastfm within a React app.
I have installed it with
npm install lastfm
The official doc says it should be used like this :
var LastFmNode = require('lastfm').LastFmNode;
var lastfm = new LastFmNode({
api_key: 'abc',
secret: 'secret'
});
And this is how I use it in my React code :
import {LASTFM_API,LASTFM_API_SECRET} from "./Constants";
import {LastFmNode} from 'lastfm';
export class Lastfm {
static scrobbleTrack(track): void {
var lastfm = new LastFmNode({
api_key: LASTFM_API,
secret: LASTFM_API_SECRET,
useragent: 'appname/vX.X MyApp'
});
}
}
Which fires this error (from the package) :
TypeError: require is not a function at lastfm-request.js:3
I'm quite new to React and NPM.
I wonder if there is some kind of incompatibility ?
How could I make this work ?
Thanks !
The package is to be used on Node server and not on React App.
EDIT
Hi, sorry i cannot comment because my points are less.
You can not use that one as well because that is also a node package. If you see a syntax where you have to use: require('something'); usually means it is a node package because in React, your syntax for importing is something like this: const abc from 'xyz';

typescript express get does not work in separate files

I'm currently converting my node code to node-ts to help our development flow for someone that is going to help out.
I tried using this information (typescript node.js express routes separated files best practices) initially but that didn't work, so as of right now I have this below
In my index.ts I have this on the bottom
import Auth from "./routes/Auth";
app.use('/auth', Auth.Routing());
export = app;
Then in my ./routes/Auth.ts I have this
const Routing = function() {
app.get('/session', User.session);
return app;
}
export = { Routing };
When I try to access /auth/session all it returns is index_1.default.get.
Using the link above, I attempted to use const router = express.Router(); and then export = router and what not but was unable to get it to work for that either with the same error.

How can my client get application configuration from the server when using Webpack?

I'm adding Webpack to a Node/Express app that previously used RequireJS. When the client needed some configuration from the server, we previously used a custom Express route that retrieved specific configs as JSON:
server/index.js - Set up Express routes for config files
const app = express();
const configRouter = express.Router();
configRouter.get('/some-config.json', (req, res) => {
const someConfig = {
prop1: getProp1(),
prop2: getProp2()
}
res.json(someConfig);
}
app.use('/config', configRouter);
client/controller.js - Use/config/some-config.json during initialization
define(['text!/config/some-config.json'], function(SomeConfig) {
// do something with SomeConfig
});
But removing RequireJS means I can no longer retrieve the JSON this way as a dependency. And it's not static JSON either, so it's not as simple as just placing it alongside client code and importing it.
So what is the best way to do this with Webpack? Any help greatly appreciated. Thanks!

Access to aws-lambda context when running nodejs + expressjs

I'm, just starting out with AWS-Lambda, AWS-API Gateway and ExpressJs. I'm having trouble finding how the AWS-Lambda "context" is available in my "ExpressJs" application.
I'm using:
AWS-Lambda
AWS-API Gateway
NodeJs v4.3.2
ExpressJs 4.14.1
ClaudiaJs 2.7.0
In Aws Lambda I use aws-serverless-express to receive the API-Gateway request and initialize the node application. The following is the structure I have found from different tutorials, etc
lambda.js (Initiated from API-Gateway. Supplying the "context" variable in the call to "app.js")
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)
The core of my app.js express is:
var express = require('express');
...
var app = express();
...
app.use('/', index);
...
module.exports = app;
My questions:
Is there a way to access the AWS-Lambda "context" with this
structure?
If not, what would be the best "pattern" to make it available?
Any input appreciated.
You need to add middleware included in the aws-serverless-express package which exposes the event and context objects. You add it like this:
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
app.use(awsServerlessExpressMiddleware.eventContext())
Once this middleware is configured the event and context objects will be added to the request. You access those objects like so:
var event = req.apiGateway.event;
var context = req.apiGateway.context;

Resources