0

I set app.js,androuterin node.js I start app.js like node app.js

https://5a75307007e3415f9aaca9d3052ed731.vfs.cloud9.us-east-2.amazonaws.com/

But I suffered like following.

No application seems to be running here!

Are there wrong point in following..

As I am a entry level,I would like to know how to check where is the wrong point..

Thanks

app.js

const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const Router = require('./routes/routes');
const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'ejs');
app.use(express.static("public"));


app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', Router);
app.use('/quiz',Router);


module.exports = app;

routes

var express = require("express");
var router = express.Router();
var quizController = require("../controllers/QuizController");

router.get('/',(req,res)=>{
    res.render("index");
});
router.get('/quiz',quizController.doGetQuiz);

module.exports = router;
Heisenberg
  • 4,787
  • 9
  • 47
  • 76

1 Answers1

1

None of the code you show actually starts your server with app.listen(somePort), so there's no lasting anything in your app so it runs app.js and then nodejs quits because there's nothing else to do. You created an app object and set some state in it, but then never started the server so the app just exits on its own with nothing else to do.

So, add:

app.listen(somePort);

where somePort is whatever port you want your server to run on and that will actually start your server so it can receive incoming requests and so the app will stay running.


Also, it doesn't make sense to have both of these:

app.use('/', Router);
app.use('/quiz',Router);

Probably, you should only have the first line since that router already has a route for /quiz in it.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks, I'd like to understand of duplicate in `app.use('/', Router); app.use('/quiz',Router);` ,why `app.use('/quiz',Router);` will not be needed. When I access`/quiz` it seems to be set in `route` and `app.js` – Heisenberg Aug 09 '20 at 06:50
  • @Heisenberg - You already have `router.get('/quiz',quizController.doGetQuiz);` in your router which gives you a route handler for `/quiz` already. – jfriend00 Aug 09 '20 at 15:36