MTD*_*MTD 7 node.js angularjs mean-stack grunt-nodemon
编辑 经过进一步的测试,我发现在这个应用程序和默认安装的mean.js上都会发生这种情况.我在Mac上本地运行它.当我使用"node server.js"运行任一app时,它们不会崩溃.
我正在使用带有grunt-nodemon的MEAN堆栈,并且当访问快速URL时节点崩溃.但它并不总是一致的.有时它可以工作,有时当URL被命中时节点崩溃而没有数据,有时我得到响应并且节点在之后立即崩溃.
浏览器控制台响应
http://localhost:8000/api/users net::ERR_CONNECTION_REFUSED
Run Code Online (Sandbox Code Playgroud)
终端输出:
Mongoose: users.insert({ firstname: 'mike', lastname: 'jones', email:'mike@gmail.com', role: 'admin', password: 'mike', _id: ObjectId("57485c16fc11894b96c28057"), created: new Date("Fri, 27 May 2016 14:39:18 GMT"), __v: 0 })
user.save success
node crash
[nodemon] app crashed - waiting for file changes before starting...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,POST请求通过,添加了用户,然后节点崩溃,但有时它会在POST成功之前崩溃.节点偶尔也会在GET请求中崩溃.
gruntfile.js:
module.exports = function(grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
var pkg = grunt.file.readJSON('package.json');
var options = {
paths: {
app: 'app',
assets: 'app/assets',
dist: 'app/dist',
distAssets: 'app/dist/assets',
html: 'app/html',
htmlTmp: '.tmp/htmlsnapshot',
htmlAssets: 'app/html/assets',
index: 'app/dist/index.html',
indexDev: 'app/index.html',
indexTmp: '.tmp/html/index.html'
},
pkg: pkg,
env: {
test: {
NODE_ENV: 'test'
},
dev: {
NODE_ENV: 'development'
},
prod: {
NODE_ENV: 'production'
}
}
};
// Load grunt configurations automatically
var configs = require('load-grunt-configs')(grunt, options);
// Define the configuration for all the tasks
grunt.initConfig(configs);
// Connect to the MongoDB instance and load the models
grunt.task.registerTask('mongoose', 'Task that connects to the MongoDB instance and loads the application models.', function () {
// Get the callback
var done = this.async();
// Use mongoose configuration
var mongoose = require('./config/lib/mongoose.js');
// Connect to database
mongoose.connect(function (db) {
done();
});
});
grunt.registerTask('bumper', ['bump-only']);
grunt.registerTask('css', ['sass']);
grunt.registerTask('default', [
'sass',
'copy:dev',
'nodemon',
'concurrent:dev',
'watch',
'mongoose'
]);
grunt.registerTask('shared', [
'clean:demo',
'copy:demo',
'sass',
'ngconstant',
'useminPrepare',
'concat:generated',
'cssmin:generated',
'uglify:generated',
'filerev',
'usemin',
'imagemin',
'usebanner'
]);
grunt.registerTask('demo', [
'shared',
'copy:postusemin',
'grep:demo'
]);
grunt.registerTask('dist', [
'shared',
'copy:postusemin',
'copy:dist',
'grep:dist',
'compress',
'copy:postusemin',
'grep:demo',
]);
grunt.loadNpmTasks('grunt-forever');
};
Run Code Online (Sandbox Code Playgroud)
default.js
module.exports.tasks = {
// version update
bump: {
options: {
files: ['package.json', 'bower.json'],
pushTo: 'origin'
}
},
// application constants
ngconstant: {
options: {
dest: '<%= paths.assets %>/js/app.constants.js',
name: 'app.constants',
}
},
// remove all bs from css
cssmin: {
options: {
keepSpecialComments: 0
}
},
markdown: {
all: {
files: [
{
src: 'README.md',
dest: '<%= paths.assets %>/tpl/documentation.html'
}
],
options: {
template: '<%= paths.assets %>/tpl/_documentation_template.html',
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
dev.js:
var _ = require('lodash'),
defaultAssets = require('./assets/default'),
testAssets = require('./assets/test'),
testConfig = require('./env/test'),
fs = require('fs'),
path = require('path');
module.exports.tasks = {
// copy files to correct folders
copy: {
dev: {
files: [
{ expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/font-awesome/fonts', dest: '<%= paths.assets %>/fonts' },
{ expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/material-design-iconic-font/fonts', dest: '<%= paths.assets %>/fonts' },
{ expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/roboto-fontface/fonts', dest: '<%= paths.assets %>/fonts' },
{ expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/weather-icons/font', dest: '<%= paths.assets %>/fonts' },
{ expand: true, src: '**', cwd: '<%= paths.app %>/bower_components/bootstrap-sass/assets/fonts/bootstrap', dest: '<%= paths.assets %>/fonts' }
]
}
},
// watch for changes during development
watch: {
js: {
files: ['Gruntfile.js', '<%= paths.assets %>/js/**/*.js'],
tasks: ['jshint'],
options: {
livereload: true
}
},
css: {
files: [
'<%= paths.assets %>/css/**/*.scss'
],
tasks: ['sass'],
options: {
livereload: true
}
},
markdown: {
files: [
'README.md'
],
tasks: ['markdown']
},
tasks: [ 'express:dev' ],
},
// debug while developing
jshint: {
all: ['Gruntfile.js', '<%= paths.assets %>/js/**/*.js']
},
concurrent: {
dev: {
tasks: ['nodemon', 'node-inspector', 'watch'],
options: {
logConcurrentOutput: true
}
}
},
nodemon: {
dev: {
script: 'server.js',
options: {
nodeArgs: ['--debug'],
ext: 'js,html',
callback: function (nodemon) {
nodemon.on('crash', function (event) {
console.log(event);
});
},
watch: _.union(defaultAssets.server.gruntConfig, defaultAssets.server.views, defaultAssets.server.allJS, defaultAssets.server.config)
}
}
},
forever: {
server1: {
options: {
index: 'server.js',
//logDir: 'logs'
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
角度控制器功能:
$scope.addUser = function(){
var user = {
firstname: $scope.firstname,
lastname: $scope.lastname,
email: $scope.email,
role: $scope.role.selected,
password: $scope.password
};
$http.post('/api/userAdd', user ).then(function successCallback(response) {
$location.path('/users');
}, function errorCallback(response) {
console.log('error addding user');
console.log(response);
});
};
Run Code Online (Sandbox Code Playgroud)
快递路线:
User = require('../models/user.js');
module.exports = function (app) {
app.get('/api/users', function (req, res) {
User.find({}, function (err, users) {
if ( err ) {
res.send({
message : 'error finding users',
success: false
});
} else {
res.json(users);
}
});
});
app.get('/api/users', function (req, res) {
User.find({fields: {}}, function (err, docs) {
res.json(docs);
});
});
app.post('/api/userAdd', function (req, res) {
var user = new User(req.body);
user.save( function( err, user ){
if (err){
console.log('user.save error');
console.log(err);
res.send({
success: false
});
} else {
console.log('user.save success');
res.send({
success: true
});
}
});
});
};
Run Code Online (Sandbox Code Playgroud)
我还使用Chromes Advanced REST扩展进行测试,并且使用此工具节点的任何请求都会立即崩溃.
我是MEAN的新手所以我错过了导致崩溃的事情吗?有任何想法吗?
as8*_*297 43
这是因为所有在后台运行的服务器进程。所以你需要做的就是从终端阻止它们。
快招
对于 LINUX
通过在终端上运行这个来杀死他们:
pkill -f node
Run Code Online (Sandbox Code Playgroud)
然后重启nodemon。
FOR 窗口
1. Go to the task manager
2. Then search-for node.js server-side javascript
3. Then right click on it and end-task from the processes.
Run Code Online (Sandbox Code Playgroud)
然后重启服务器。它会正常工作。
Siv*_*iva 11
在终端中使用以下命令终止现有正在运行的端口并重新启动服务。
killall -9 node
Run Code Online (Sandbox Code Playgroud)
或者杀死特定端口而不是所有端口
sudo lsof -i :3000 //replace 3000 with your port number
sudo kill -9 31363 // replace 31363 with your PID
Run Code Online (Sandbox Code Playgroud)
小智 5
问题: 应用程序崩溃 - 在开始之前等待文件更改...
可能有不同的原因:
1. 后台运行节点
pkill -f node
在 Mac OS 中工作,在 Microsoft 中没有工作。
2. Server.js 和 package.json 不在同一个文件夹中。核实 。
检查 package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon Server.js"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19468 次 |
| 最近记录: |