我在angularjs
和nodejs
制作了一个平均应用。该应用程序在本地运行良好,前端和后端都在不同的端口上运行。但在生产时,我的前端只在端口8100
上运行。虽然我的节点应用程序在指定的端口上运行正常。
项目目录
我的目录结构在生产和本地
上都是这样的--app
| ---assets
---bower_components
---directives
---home
---misc
---product
---services
---user
---index.html
--backend
| ---img
---models
---node_modules
---public
---routes
---app.js
---package.json
--package.json (frontend)
包jso前端
{
"name": "secret-project",
"private": true,
"version": "0.0.0",
"description": "Secret Project",
"repository": "https://github.com/angular/angular-seed",
"license": "MIT",
"devDependencies": {
"bower": "^1.7.7",
"http-server": "^0.9.0",
"jasmine-core": "^2.4.1",
"karma": "^0.13.22",
"karma-chrome-launcher": "^0.2.3",
"karma-firefox-launcher": "^0.1.7",
"karma-jasmine": "^0.3.8",
"karma-junit-reporter": "^0.4.1",
"protractor": "^4.0.9"
},
"scripts": {
"postinstall": "bower install",
"update-deps": "npm update",
"postupdate-deps": "bower update",
"prestart": "npm install",
"start": "http-server -a x.x.x -p 8100 -c-1 ./app",
"pretest": "npm install",
"test": "karma start karma.conf.js",
"test-single-run": "karma start karma.conf.js --single-run",
"preupdate-webdriver": "npm install",
"update-webdriver": "webdriver-manager update",
"preprotractor": "npm run update-webdriver",
"protractor": "protractor e2e-tests/protractor.conf.js",
"update-index-async": "node -e \"var fs=require('fs'),indexFile='app/index-async.html',loaderFile='app/bower_components/angular-loader/angular-loader.min.js',loaderText=fs.readFileSync(loaderFile,'utf-8').split(/sourceMappingURL=angular-loader.min.js.map/).join('sourceMappingURL=bower_components/angular-loader/angular-loader.min.js.map'),indexText=fs.readFileSync(indexFile,'utf-8').split(/\\/\\/@@NG_LOADER_START@@[\\s\\S]*\\/\\/@@NG_LOADER_END@@/).join('//@@NG_LOADER_START@@\\n'+loaderText+' //@@NG_LOADER_END@@');fs.writeFileSync(indexFile,indexText);\""
},
"dependencies": {
"ngcli-task-runner": "^1.0.3"
}
}
我的app.js(节点)
// importing modules
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cors = require('cors');
var nodemailer = require('nodemailer');
//path is a core module its already installed
var path = require('path');
//Use express methoods
//express
var app = express();
//port no
const port = 3000;
//getting routes defined in route js
const route = require('./routes/route');
//connect to mongo db300
mongoose.connect('mongodb://121.658.153:27017/bit-manage-backend', {
useMongoClient: true
});
//on connection
mongoose.connection.on('connected', () => {
console.log('------------------------------------');
console.log('connected to db');
console.log('------------------------------------');
});
mongoose.connection.on('error', (err) => {
if (err) {
console.log('------------------------------------');
console.log(err);
console.log('------------------------------------');
}
});
//cors
app.use(cors());
var allowCrossDomain = function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
next();
}
//body-parser - parse all our json data
app.use(bodyParser.json());
//static files
app.use(express.static(path.join(__dirname, 'public')));
//calling api , all the routes starting with api are directed to route file
app.use('/api', route);
app.use(allowCrossDomain);
//bind server with port
app.listen(port, () => {
console.log('------------------------------------');
console.log('server is running' +
port);
console.log('------------------------------------');
})
现在,如果我在package.json中指定除8100以外的任何其他端口,则它不起作用。我正在做的错误是什么?我想在端口80上运行它,这是默认端口。所以当我从行"start": "http-server -a x.x.x -c-1 ./app",
中删除端口时。它仍然无效。
注意 我在生产服务器上这样做而不是在本地服务器上。