gpt4 book ai didi

javascript - 编写 Node.js 代码时,app.send 不是 WebStorm 中的函数错误

转载 作者:行者123 更新时间:2023-12-03 03:25:21 24 4
gpt4 key购买 nike

我是 Node.js 的初学者,并且仍在学习教程。我做了与教程建议的完全相同的事情,但仍然出现此错误。这是我在每一行和接下来的代码中不断收到的错误。

用WebStorm编写的代码,我只是修改了其中的两行:

 const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

const app = express();

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

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
app.send('This is a Node Program.');
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;

错误:

app.send is not a function

TypeError: app.send is not a function
at C:\Users\Tuladhar\WebstormProjects\NodeDemo\app.js:23:8
at Layer.handle [as handle_request] (C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\express\lib\router\index.js:275:10)
at SendStream.error (C:\Users\Tuladhar\WebstormProjects\NodeDemo\node_modules\serve-static\index.js:121:7)
at emitOne (events.js:115:13)

有人可以帮我解决这个错误吗?我可能缺少一些库文件或其他东西。有人可以帮助我修复这个错误并建议我应该做什么来防止这些错误吗?

最佳答案

我认为你必须像这样使用“res”对象发送:

res.send('This is a Node Program.');

希望有帮助。

关于javascript - 编写 Node.js 代码时,app.send 不是 WebStorm 中的函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46358286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com