gpt4 book ai didi

angularjs - Angular + Node.js HTTP 和 HTTPS (SSL)

转载 作者:搜寻专家 更新时间:2023-11-01 00:42:36 24 4
gpt4 key购买 nike

我想在我的付款 View 中对发布请求使用 https。我偶然发现了 diff 教程,现在我的设置如下(但我仍然无法运行):

证书

我使用本教程为我的开发环境生成了证书: http://greengeckodesign.com/blog/2013/06/15/creating-an-ssl-certificate-for-node-dot-js/

Node .js

我按照答案 #2 在 Node 中设置 http 和 https 服务器: Listen on HTTP and HTTPS for a single express app

看起来像这样:

environment = require('./config/config.js')()

express = require('express')
bodyParser = require('body-parser')
app = express()
portHTTP = process.env.PORT || 61361
portHTTPS = 3030
mongoose = require('mongoose')
morgan = require('morgan')
http = require('http')
https = require('https')
socket = require('socket.io')

#SSL Server
fs = require('fs')
sslOptions = {
key: fs.readFileSync(__dirname + '/config/ssl/server.key'),
cert: fs.readFileSync(__dirname + '/config/ssl/server.crt'),
ca: fs.readFileSync(__dirname + '/config/ssl/ca.crt'),
requestCert: true,
rejectUnauthorized: false
}

#Database
configDB = require('./config/database.js')(environment)
mongoose.connect(configDB.url,{auth:{authdb:configDB.authdb}}, (err)->
if (err)
console.log(err)
)
db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', () ->
console.log "Database established"
)

#Express Application
app.use(morgan('dev'))# log every request to the console
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

rootPath = __dirname + '/../../'
app.use('/bower_components', express.static(rootPath + 'bower_components'))
app.use('/partials', express.static(__dirname + '/../partials'))

# Routes
require('./node/routes.js')(app, db) # load our routes and pass in our app and fully configured passport

# Launch the application
https.createServer(sslOptions,app).listen(portHTTPS, () ->
console.log("Secure Express server listening on port #{portHTTPS}")
)
http.createServer(app).listen(portHTTP)
io = socket.listen(https)

#app.listen(portHTTP)
console.log('Unsecure Express server listening on port #{portHTTP} environment: #{environment}')

Angular

我安装了 Angular 模块: https://github.com/btford/angular-socket-io

并将其添加到我的 index.coffee

angular.module "app", [lots of dependencies, 'btford.socket-io', 'myCodeService']
.config ($stateProvider, $urlRouterProvider) ->
...states...
.factory('mySocket', (socketFactory) ->
return socketFactory({
ioSocket: io.connect('https://localhost:3030/',{secure: true})
#ioSocket: io.connect('https://cdn.socket.io/socket.io-1.3.4.js',{secure: true})
})
)

我在 index.html 中添加了两个脚本标签

<script src="../bower_components/angular-socket-io/socket.js"></script>
<script src="http://localhost:3030/socket.io/socket.io.js"></script>

Gulp 设置

我用 nodemon 和 browsersync 服务

gulp.task('serve',['watch'], function(cb){
var called = false;
return nodemon({
script: paths.tmp + '/serve/server.js',
watch: [paths.src + '/node', paths.src + '/config'],
ext: 'coffee',
tasks: ['templateCache', 'partials_tmp'],
env: { 'NODE_ENV': 'development' } ,
nodeArgs: ['--debug=9999']
})
....

function browserSyncInit(files, browser) {
browser = browser === undefined ? 'default' : browser;
browserSync.instance = browserSync.init(files, {
startPath: '/#/',
browser: browser,
proxy: "http://localhost:61361"
});
}

我从 socket.io 得到一个空的响应,我在浏览器中没有看到任何让我接受证书的东西。

browsersync 有一个 https 选项,但我真的不知道如何拆分流量/或者这是否在 node.js 中自动完成。

最佳答案

io = socket.listen(https)是不正确的。您应该传递服务器实例,而不是 https模块:

// ...
var srv = https.createServer(sslOptions,app);
srv.listen(portHTTPS, () ->
console.log("Secure Express server listening on port #{portHTTPS}")
)
io = socket.listen(srv);
// ...

还有你的<script> src指向socket.io.js应该是 https而不是 http .

关于angularjs - Angular + Node.js HTTP 和 HTTPS (SSL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29629673/

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