我已经在Bitnami制作的aws实例上上传了一个MEAN堆栈项目,并在htdocs上设置了Angular的dist,其中默认索引为html.html。当我使用节点index.js启动服务器并将它与mongodb连接时,它可以工作,但是当我尝试执行任何请求(如登录/注册)时,我的状态为500。
我尝试授予所有文件所有权限,并按如下方式配置.htaccess:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
Options +ExecCGI
AddHandler cgi-script .cgi .py .php .pl .js
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
我在控制台上看到的错误是
对象{标头:{…},状态:0,statusText:“未知错误”,url:“ http://socializeweb.es:3000/api/login”,ok:否,名称:“ HttpErrorResponse”,消息:“ { {3}}:0未知错误”,错误:错误
Index.js代码
// Requires and const definition
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const mongoURI = "mongodb+srv://***:***@socialize-vgoeq.mongodb.net/test?retryWrites=true&w=majority";
const http = require('http');
const normalizePort = require('normalize-port');
const locationPicker = require("location-picker")
// Configuración CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});
// Middlewares
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
// Routes
const user_routes = require('./routes/user.routes');
const follow_routes = require('./routes/follow.routes');
const group_routes = require('./routes/groups.routes');
const publication_routes = require('./routes/publication.route');
const group_follows_routes = require('./routes/groupFollows.routes');
const messages = require('./routes/messages.route');
app.use('/api', user_routes);
app.use('/api', follow_routes);
app.use('/api', group_routes);
app.use('/api', publication_routes);
app.use('/api', group_follows_routes);
app.use('/api', messages);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
io.on('connection', (socket) => {
socket.on('join', function (data) {
//joining
if (data.room) {
socket.leave(socket.room);
socket.broadcast.to(data.room).emit('left room', {
user: data.user,
message: 'ha abandonado la sala.'
});
}
socket.join(data.room);
console.log(data.user + 'joined the room : ' + data.room);
socket.broadcast.to(data.room).emit('new user joined', {
user: data.user,
message: 'ha entrado en la sala.'
});
});
socket.on('message', function (data) {
io.in(data.room).emit('new message', {
user: data.user,
message: data.message
});
})
});
// Database
mongoose.Promise = global.Promise;
mongoose.set('useFindAndModify', false);
mongoose.connect(mongoURI, {
useNewUrlParser: true
})
.then(() => {
console.log("BD conectada");
console.log("Node running on " + port)
// Creación del servidor
server.listen(port);
})
.catch(err => console.log(err));
前后之间的连接是通过这样的服务建立的
@Injectable()
export class UserService {
public url: string;
public identity;
public token;
stats: any;
constructor(public _http: HttpClient) {
this.url = "http://" + window.location.hostname + ":3000/api/";
}
signin(user: User, gettoken = null): Observable<any> {
if (gettoken != null) {
user.gettoken = gettoken;
}
let params = JSON.stringify(user);
let headers = new HttpHeaders().set('Content-Type', 'application/json');
}