将节点从v6更新到v8后,nodejs项目无法正常工作

时间:2018-02-05 06:52:22

标签: node.js mongodb express

当节点版本为6.11.2时,我的项目工作正常。将其更新为8后,它没有在邮递员中给出任何响应,它继续加载。为什么会这样?

如果我在app.js中控制某些内容,则会打印值,但不会在media.js中打印。

文件夹结构

backend -routes -api media.js app.js



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

/* global config,mongoskin,db */

global.config = require('./config');

var session = require('express-session');
var MongoStore = require('connect-mongo')(session);

var middlewares = require('./middlewares');
global.user_auth = middlewares.user_auth;
global.artist_auth = middlewares.artist_auth;
global.admin_auth = middlewares.admin_auth;
global.nocache = middlewares.nocache;

global.__base = __dirname;

global.lib = require('./lib');

// database
global.mongoskin = require('mongoskin');


var app = express();
app.use('/api/', function(req, res, next) {
    var origin = req.headers.origin;
    console.log(origin);
    console.log('hello');
    if (!origin) {
        res.header('Access-Control-Allow-Origin', 'http://wd-agency.com/2784');
    } else {
        res.header('Access-Control-Allow-Origin', origin);
    }
    //res.setHeader('Cache-Control', 'public, max-age=31557600');// One Year
    res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
    res.header('Expires', '-1');
    res.header('Pragma', 'no-cache');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', ['GET', 'PUT', 'POST', 'DELETE']);
    next();
});

app.disable('x-powered-by');
config.env = app.get('env');

var connection_str = 'mongodb://@localhost:27017/wd_artifu';
if (config.env == 'development') {
    connection_str = 'mongodb://@localhost:27017/wd_artifu_dev';
    /// Override console.log for dev
    var _log = console.log;
    global.console.log = function() {
        var traceobj = new Error("").stack.split("\n")[2].split(":");
        var file = traceobj[0].split(process.env.PWD + '/')[1];
        var line = traceobj[1];
        var new_args = [file + ":" + line + " >>"];
        new_args.push.apply(new_args, arguments);
        // if(arguments[0]==true)
        _log.apply(null, new_args);
    };
}

global.db = mongoskin.db(connection_str);
db.bind('users');
db.bind('conversations');
db.bind('media');
db.bind('arts');
db.bind('complaints');
db.bind('wishlist');
db.bind('invoice');
db.bind('payout');
db.bind('carts');
db.bind('feedback');
db.bind('revenue');
db.bind('languages');

global.db.users.findOne({
    type: 'admin'
}, function(err, doc) {
    if (err) {
        console.log(err.stack);
        process.exit();
    }
    if (!doc) {
        console.log('creating admin');
        var admin = {
            name: 'admin', 
            type: 'admin',
            email: 'artifu_admin@wd-agency.com',
            password: global.lib.getSaltedPassword('adminWD2015')
        };
        global.db.users.save(admin, function(err) {
            if (err) {
                console.log(err.stack);
                process.exit();
            }
        });
    }
});

global.db.languages.findOne({
    name: 'English'
}, function(err, exist) {
    if (err) {
        console.log(err.stack);
        process.exit();
    }
    if (!exist) {
        console.log('creating data for English');
        fs.readFile('../language.json', 'utf8', function(err, data) {
            if (err) {
                console.log("err", err);
            }
            var objects = JSON.parse(data);
            global.db.languages.save(objects, function(err) {
                if (err) {
                    console.log(err.stack);
                    process.exit();
                }
            });
        })
    } else {
        console.log('English language already created ');
    }
});

// uncomment after placing your favicon in /public
// app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.use(cookieParser());

app.use(session({
    resave: true,
    saveUninitialized: true,
    secret: '3478129048712904871239084',
    store: new MongoStore({
        db: global.db
    })
}));

app.use(function(req, res, next) {
    if (req.method != 'GET') console.log(req.body); // or, just when validation failes..
    try {
        req.body = JSON.parse(Object.keys(req.body)[0]); // wordaround for cors
        if (req.method !== 'GET') {
            console.log(req.body);
        }
        req.errors = [];
        next();
    } catch (e) {
        next();
    }
});
app.use(require('compression')()); //XXX use apache's compression in prod.
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', express.static(path.resolve(__dirname, '..', 'angular', 'app')));
app.use('/admin', express.static(path.resolve(__dirname, '..', 'admin_ui')));
app.use(logger('dev'));



//main page..

app.use('/api/', require('./routes/api'));

// activate html5Mode

app.all('/*', function(req, res, next) {
    res.sendFile(path.join(__dirname, '../angular/app/', 'index.html'));
});

// catch 404 and forward to error handler
app.use(function(req, res) {
    console.log("generic 404 handler: " + req.originalUrl);
    res.status(404).send({
        status: 404,
        type: 'generic'
    });
});

if (config.env === 'development') {
    app.use(function(err, req, res) {
        console.log(err.stack);
        res.status(err.status || 500);
        res.send(err);
    });
}

// production error handler
// no stacktraces leaked to user
else {
    app.use(function(err, req, res) {
        res.status(err.status || 500);

        res.render('error', {
            message: err.message,
            error: {}
        });
    });
}



global.config = config;
module.exports = app;




这是我的app.js文件。如果我在app.js中控制某些内容将打印值,但不会在media.js中打印。

0 个答案:

没有答案