search

Home  >  Q&A  >  body text

Nodejs error TypeError: this.set is not a function

Hi everyone, smart people, I am a beginner here and I have a problem with my code while working on the server side. I really need help.

const express = require('express');

const app = express();

app.set = ('view engine', 'ejs');

app.use(express.static('public'))
app.use(express.urlencoded());


app.get('/', async (req, res)=>{
   res.render('index.ejs')
})

app.get('/addstock', (req, res)=>{
    res.render('addstock.ejs')
})


app.listen(8080, ()=>{
    console.log('server is currently on port 8080')
})

The following is the response I got:

C:\Users\go\Desktop\New folder\WEB project\kemstac\node_modules\express\lib\application.js:439 return boolean(this.set(set)); ^

Type error: this.set is not a function in Function.enabled (C:\Users\go\Desktop\New Folder\WEB Project\kemstac\node_modules\express\lib\application.js:439:23) in Function.lazyrouter (C:\Users\go\Desktop\New Folder\WEB Project\kemstac\node_modules\express\lib\application.js:147:27) in Function.use (C:\Users\go\Desktop\New Folder\WEB Project\kemstac\node_modules\express\lib\application.js:221:8) in object. (C:\Users\go\Desktop\New folder\WEB project\kemstac\app.js:16:5) at Module._compile(node:internal/modules/cjs/loader:1255:14) in Module._extensions..js (node:internal/modules/cjs/loader:1309:10) at Module.load(node:internal/modules/cjs/loader:1113:32) in Module._load (node:internal/modules/cjs/loader:960:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) In node:internal/main/run_main_module:23:47

Node.js v20.2.0 [nodemon] Application crash - waiting for file changes before starting...

P粉757640504P粉757640504233 days ago502

reply all(1)I'll reply

  • P粉352408038

    P粉3524080382024-04-01 10:39:05

    The problem is with the line where you set the view engine in Express.

    app.set = ('view engine', 'ejs');

    The corrected code looks like this:

    app.set('view engine', 'ejs');

    reply
    0
  • Cancelreply