node-windows配置

发布于 2022-09-06 19:24:58 字数 4486 浏览 14 评论 0

使用node-windows配置使得开机自启动node服务,项目是node+express搭建的,使用的es6,用babel编译,平常开启时这样的:

 "scripts": {
    "build": "babel ./bin/www -d lib",
    "start": "nodemon ./bin/www --exec babel-node --presets es2015,stage-0"
  },

然后node-windows的nw.js是这样的

let Service = require('node-windows').Service

let svc = new Service({
  name: 'test',
  description: 'test',
  script: './bin/www'
})

svc.on('install', () => {
  svc.start()
  console.log('install complete.')
})

svc.install()

请问有什么方法可以在es6下让node nw.js开启?错误日志如下:

F:\test\app.js:1
(function (exports, require, module, __filename, __dirname) { import express from 'express'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (F:\test\bin\www:7:11)
    at Module._compile (module.js:570:32)
F:\test\app.js:1
(function (exports, require, module, __filename, __dirname) { import express from 'express'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (F:\test\bin\www:7:11)
    at Module._compile (module.js:570:32)
F:\test\app.js:1
(function (exports, require, module, __filename, __dirname) { import express from 'express'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (F:\test\bin\www:7:11)
    at Module._compile (module.js:570:32)
C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:177
    child.send('shutdown');
         ^

TypeError: Cannot read property 'send' of null
    at process.killkid (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:177:10)
    at emitOne (events.js:96:13)
    at process.emit (events.js:188:7)
    at process.exit (internal/process.js:164:15)
    at monitor (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:99:17)
    at ChildProcess.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:170:5)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:219:12)

app.js部分:

import express from 'express'
import path from 'path'
import logger from 'morgan'

let app = express()
global.app = app

//  设置访问请求头,解决访问跨域问题
app.all('*', (req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Header', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
  res.header('X-Powered-By', '4.15.5')

  if(req.method === 'OPTIONS') res.send(200)
  else next()
})

//  视图模板
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')

app.use(logger('dev'))
app.use(express.static(path.join(__dirname, 'public')))

let routes = require('./routes/index')
//  404
app.use( (req, res, next) => {
  let err = new Error('Not Found')
  err.status = 404
  next(err)
})

//  错误处理
app.use( (err, req, res) => {
 
  res.locals.message = err.message
  res.locals.error = req.app.get('env') === 'development' ? err : {}

  res.status(err.status || 500)
  res.render('error')
})

module.exports = app

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文