gpt4 book ai didi

node.js - Node.js 中 app.js 和 index.js 的区别

转载 作者:IT老高 更新时间:2023-10-28 23:25:50 27 4
gpt4 key购买 nike

我是 Nodejs 的新手。我在 route 目录中有一个 app.js 和一个 index.js。我有一个 app.use(multer....)。我还定义了 app.post('filter-reports') ,它实际上将文件内容上传到服务器。

我有要执行的业务逻辑,并已在 routes/index.js 文件中配置路由,我打算在该文件中配置 /filter-reports 路由。请帮助我了解我哪里出错了。我需要使用 multer 上传文件,同时运行 index.js 文件中的业务逻辑。

app.js 源码:

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var _ = require('underscore');
var cache = require('js-cache');
var multer = require('multer');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});


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

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
var done=false;

app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
done=true;
}
}));

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

app.post('filter-reports',function(req,res){
console.log('Working on the filtered reports....');
if(done==true){
console.log(req.files);
res.end("File uploaded.");
}
});

/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;





var express = require('express');
var request = require("request");
var _ = require('underscore');
var jscache = require('js-cache');
var schedule = require('node-schedule');
var filename;

在 index.js 文件中

var router = express.Router();

router.post('/filter-reports', function(req, res) {
console.log('Came inside the Node js router.. Now.. its all up to me to format the data....');
// console.log(req.files);
// console.log('Came insode the filter-reports app url >>>>');
// if(done==true){
// console.log(req.files);
// console.log('Files uploaded succesfully ....');
//res.end("File uploaded.");
// }
});

我的包结构如下:

app.js  bin  node_modules  package.json  public  routes  views

我的 package.json 是

{
"name": "nodetest1",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.2.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"multer": "~0.1.6",
"jade": "~1.3.0"
}
}

提前致谢,普雷迪普

最佳答案

当您将文件夹传递给 Node 的 require() 时,它将检查端点的 package.json。如果没有定义,它会检查 index.js,最后检查 index.node(一种 C++ 扩展格式)。所以 index.js 很可能是需要模块的入口点。

您可以在这里查看http://nodejs.org/api/modules.html#modules_folders_as_modules

通常我使用 app.js 作为应用程序主入口点。

关于node.js - Node.js 中 app.js 和 index.js 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776453/

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