2016-05-05 2 views
0

私はエクスプレス内に独立したアプリを持っており、独自のnotFounderrorハンドラを持つ方法を探しています。私はすべてのルートをカプセル化して、後続のルートに影響を与えないようにする方法が必要です。1つの中に2つのエクスプレスアプリをカプセル化する

私は直接のアプリを使ってみました

const apiOne = express() 
apiOne.get('/api-one/hello', helloApiOne) 
apiOne.use(notFoundHandler) 
apiOne.use(errorHandler) 

const apiTwo = express() 
apiTwo.get('/api-two/hello', helloApiTwo) 
apiTwo.use(notFoundHandler) 
apiTwo.use(errorHandler) 

const app = express() 
app.get(apiOne) 
app.use(apiTwo) 

そして私はまた、ルータを使用してみました。

const apiOne = express.Router() 
apiOne.get('/api-one/hello', helloApiOne) 
apiOne.use(notFoundHandler) 
apiOne.use(errorHandler) 

const apiTwo = express.Router() 
apiTwo.get('/api-two/hello', helloApiTwo) 
apiTwo.use(notFoundHandler) 
apiTwo.use(errorHandler) 

const app = express() 
app.get(apiOne) 
app.use(apiTwo) 

答えて

0

少し違ってマウントしましたか?私はあなたがそれらをセグメント化し、そのルータ上でエラーハンドラを最初に実行することができると思うルータを使用してください。

const apiOne = express.Router() 
apiOne.get('/hello', helloApiOne) 
apiOne.use(notFoundHandler) 
apiOne.use(errorHandler) 

const apiTwo = express.Router() 
apiTwo.get('/hello', helloApiTwo) 
apiTwo.use(notFoundHandler) 
apiTwo.use(errorHandler) 

const app = express() 
app.use('/api-one', apiOne) 
app.use('/api-two', apiTwo) 
+0

私はこれを知っています。私は、各アプリがこのようにパス名の間隔に依存しないようにするソリューションを探していました。 – ThomasReggi

+0

あなたの例では、それらはすでに独立したパス名です。それらのパスを同じパスにしたい場合は、単一のエラールートを指定し、パスまたは何らかの基準を使用している別のエラーハンドラに委任することをお勧めします – Spidy

関連する問題